【问题标题】:How to hide the recyclerview on the outside touch event?如何隐藏外部触摸事件的recyclerview?
【发布时间】:2017-02-18 23:41:20
【问题描述】:

我有一个view,我想在单击按钮/布局时显示它,并在我触摸其他地方时隐藏它。我该怎么做? 我在dispatchTouchEvent(Motion Event) 中写了一些代码,它正在工作。但是,我认为必须有另一种方法来做到这一点。

【问题讨论】:

  • “使用 RecyclerView 查看”是什么意思?你的意思是recyclerView的一个项目吗?
  • 意思是RecyclerView inside view,在这种情况下我觉得没关系
  • 查看我对另一个问题的回答:stackoverflow.com/questions/6685690/…

标签: android view


【解决方案1】:

您可以用另一个可点击的视图填充 RecyclerView 的外部,并为该视图实现 setOnTouchListener 方法。这是一个例子:

假设我们在 RelativeLayout 的顶部有 RecyclerView:

<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>

当我们点击 RecyclerView 外部时,我们希望隐藏和显示我们的 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;
            }
        });

如果你想在按钮点击时显示recyclerview,那么只需在按钮ClickListener中写recyclerView.setVisibility(View.VISIBLE)方法!

希望这会有所帮助!

【讨论】:

  • 谢谢,但是如果我在这个RelativeLayout 中有很多视图,请再创建一个view 以便更容易拦截事件,是否正确?
  • 对不起,我没听懂。你介意澄清一下你说的话吗?
  • 我认为创建view 可能会有一些副作用
  • 是的,可能有。有关详细信息,请参阅 thisthis 链接
  • 不客气! :) 据我所知,你俄语说得很好。如果我帮助你编程(加上你的英语),作为回报,你帮助我学习俄语。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多