防止触摸视图 android

Prevent touches on a View android

经过一些尝试,我可以在单击浮动按钮时生成半透明背景。现在的问题是 "new background" 只改变颜色。在此之下,我有一个回收视图,我仍然可以向上或向下滑动并与之交互。我现在需要的是防止在我可见的布局下使用 recyclerview 进行的所有操作。我唯一能做的就是:

这是实际使用的代码:

OnClickListener listener = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (DrawerActivity.instance.rootFab.isExpanded())
            {
                whiteLayout.setVisibility(View.GONE);
            }
            else
            { 
                whiteLayout.setVisibility(View.VISIBLE);

            }
            mainFab.toggle();
        }
    };

当然还有:

rootFab.setAddButtonClickListener(listener);

给它听众。所以,只需单击主晶圆厂(我使用一个包含多个晶圆厂的库),它就会显示如下布局:

----
----
 <android.support.v7.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/status"
            android:clipToPadding="false"
            android:scrollbars="vertical" />
        <LinearLayout
            android:id="@+id/semi_white_bg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white_semi_transparent"
            android:orientation="vertical"
            android:visibility="gone" >
        </LinearLayout>
---
---

如果我再次按下 fab,布局就会消失...所以我的问题是,我怎样才能做同样的事情,但点击这个背景,但没有 "touch" recyclerview 在上面?

你可以告诉Android你的观点是"clickable"。这样您的视图将消耗触摸事件并且它们不会进一步传递给您的 RecyclerView.

要将视图标记为 "clickable",只需将以下标志添加到您的 xml 布局中:android:clickable="true":

----
----
 <android.support.v7.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/status"
            android:clipToPadding="false"
            android:scrollbars="vertical" />
        <LinearLayout
            android:id="@+id/semi_white_bg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white_semi_transparent"
            android:orientation="vertical"
            android:clickable="true"
            android:visibility="gone" >
        </LinearLayout>
---
---

此外,如果您仅将视图用作背景 - 我看不出有任何理由需要重量级 LinearLayout。您可以在这里使用 View

----
----
 <android.support.v7.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/status"
            android:clipToPadding="false"
            android:scrollbars="vertical" />
        <View
            android:id="@+id/semi_white_bg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white_semi_transparent"
            android:clickable="true"
            android:visibility="gone" />
---
---