【问题标题】:Prevent touches on a View android防止触摸 View android
【发布时间】:2015-03-30 22:17:14
【问题描述】:

经过一些尝试,当我单击浮动按钮时,我能够生成半透明背景。现在的问题是“新背景”只会改变颜色。在此之下,我有一个回收视图,我仍然可以向上或向下滑动并与之交互。我现在需要的是在我可见的布局下使用 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,布局就会消失......所以我的问题是,我怎么能做同样的事情,但点击这个背景,但没有“触摸”它上面的 recyclerview?

【问题讨论】:

    标签: java android android-layout floating-action-button


    【解决方案1】:

    您可以告诉 Android 您的视图是“可点击的”。这样,您的视图将使用触摸事件,并且它们不会进一步传递给您的 RecyclerView

    要将视图标记为“可点击”,只需将以下标志添加到您的 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" />
    ---
    ---
    

    【讨论】:

    • 是的,事实上我是用视图更新代码。顺便说一句,正确!现在我的视图被标记为可点击。而且我不能再触摸 RecyclerView 了。但是我可以在视图上创建一个 onClick 方法以使如果触摸晶圆厂崩溃并且视图消失成为可能吗?即使现在是 android:clickable="true"?
    • 当然,onClickListener 还可以添加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多