1

I have a view and I want to show it when I click on button/layout and hide it when I touch somewhere else. How can I do it? I wrote some code in dispatchTouchEvent(Motion Event) and it's working. But, I think there must be another way to do it.

1 Answers1

0

You can fill the outside of your RecyclerView with another clickable view and implement setOnTouchListener method for that view. Here's an example:

Let's say we have got RecyclerView at the top of our RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:scrollbars="vertical" />

    <!--View below is just to fill the remaining space. We will use this view to catch outside touch-->

<View 
    android:id="@+id/outside_detector"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/recyclerView"
    android:clickable="true"
    android:focusable="true"/>
</RelativeLayout>

And we want to hide and show our recyclerview when we click outside of RecyclerView:

((View) findViewById(R.id.outside_detector)).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                if(arg1.getAction() == MotionEvent.ACTION_DOWN){
                    if(recyclerView.getVisibility() == View.VISIBLE){
                        recyclerView.setVisibility(View.INVISIBLE);
                    }else{
                        recyclerView.setVisibility(View.VISIBLE);
                    }
                }
                return true;
            }
        });

If you want to show recyclerview on button click, then just write recyclerView.setVisibility(View.VISIBLE) method inside button ClickListener!

Hope this helps!

SpiralDev
  • 7,011
  • 5
  • 28
  • 42
  • thaks, but if I'll have a lot of views in this `RelativeLayout` create one more `view` for easier intercepting events, is it correct? – Maria Halushko Oct 11 '16 at 10:14
  • Sorry, I didn't get it. Would you mind clarifying what you said? – SpiralDev Oct 11 '16 at 11:50
  • I thought that creating `view` might have some side effects – Maria Halushko Oct 11 '16 at 14:06
  • yes, might have. For further information refer to [this](http://stackoverflow.com/questions/16155285/how-to-handle-touch-outside-the-view-in-android) and [this](http://stackoverflow.com/questions/6685690/android-make-view-disappear-by-clicking-outside-of-it) links – SpiralDev Oct 11 '16 at 15:21
  • You are welcome! :) As far as I can see you speak russian language well. What if I help you with ur programming (plus with ur english), and, in return, you help me with my russian. – SpiralDev Oct 13 '16 at 10:34
  • I like it, I agree :) – Maria Halushko Oct 17 '16 at 11:09