在Android中,我该如何禁用SlidingDrawer后面的视图?

16

我有一个SlidingDrawer,它从屏幕底部弹出并填充大约80%的屏幕。即使SlidingDrawer视图处于焦点状态,仍然可以点击在SlidingDrawer后面的视图中的项、按钮和其他元素。当SlidingDrawer处于活动/上拉/焦点状态时,我希望禁用其后面的整个视图,以便无法接收点击和触摸。有没有一种好的方法可以禁用整个视图?我已经尝试了setEnable(false)和setClickable(false),但都不起作用。

有帮助吗?


我也使用滑动抽屉,但从未遇到过这个问题^^ - DarkLeafyGreen
你解决了这个问题吗?我也遇到了同样的问题。 - Soroush Hakami
https://dev59.com/KW435IYBdhLWcg3weQBy?lq=1 - Mertuarez
请使用此链接:https://dev59.com/YHE85IYBdhLWcg3w03Dz#31587294 - Mansukh Ahir
5个回答

13

有一种方法可以解决这个问题(我也需要一个解决方案)-获取包含内容的linearLayout并添加一个单击侦听器。让单击侦听器对单击做出响应(扔掉,不管它)-然后它就会停止传播到滑动抽屉下面的视图-对我有效,并且它不会阻塞抽屉中的其他项目。


这相当于@mansukh在下面所说的 - 但他的方法代码更少。 - Elemental

3

在布局文件中加入以下代码:android:clickable="true"

例如,在 drawer.xml 文件中:

LinearLayout 中加入 android:id="@+id/drawer_view"clickable="true"

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>      

        <LinearLayout
            android:id="@+id/drawer_view"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="start"
            android:clickable="true"
            android:background="@color/drawer_background">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:background="@drawable/order_list_selector"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="@dimen/user_image_w_h"
                    android:layout_height="@dimen/user_image_w_h"
                    android:scaleType="fitCenter"
                    android:id="@+id/drawerUserImage"
                    android:src="@drawable/ic_user_icon"
                    android:layout_gravity="center_vertical" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:layout_gravity="center_vertical"
                    android:orientation="vertical"
                    android:layout_marginLeft="5dp"
                    android:padding="5dp">

                    <TextView
                        android:id="@+id/drawerUserNameTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Mansukh Ahir"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/font_large" />

                    <TextView
                        android:id="@+id/drawerEmailIdTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/font_very_small"/>
                </LinearLayout>
            </LinearLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@color/holo_gray_light" />

            <ListView
                android:id="@+id/drawerListSlideMenu"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:choiceMode="singleChoice"
                android:dividerHeight="1dp" />
        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>

在我看来,这是布局“覆盖”彼此的最简单和最直接的解决方案 - 这使得覆盖页面的背景“吸收”点击,而不是将其传递下去。 - Elemental
1
因为我无法重写SwipeRefreshLayout方法的canChildScrollUp(),所以我使用了这个解决方案。 - Mansukh Ahir

2

Joe的答案对我没起作用。我的情况有点不同。我有一个FrameLayout,里面有两个子元素。在给定的时刻只有一个子元素需要处于“活动”状态,而当第二个子元素处于活动状态时,第一个子元素就不再处理任何输入。我的解决方案:

    public static void changeVGstate(ViewGroup current, boolean enable)
{
    current.setFocusable(enable);
    current.setClickable(enable);
    current.setEnabled(enable);

    for (int i = 0; i < current.getChildCount(); i++)
    {
        View v = current.getChildAt(i); 
        if (v instanceof ViewGroup)
            changeVGstate((ViewGroup)v, enable);
        else
        {
            v.setFocusable(enable);
            v.setClickable(enable);
            v.setEnabled(enable);
        }
    }
}

享受吧!


2

我尝试将 SlidingDrawer 放在 RelativeLayout 中,而不是 LinearLayout 中。并在打开方法中设置 mySlidingDrawer.bringToFront()

因此,我认为这可能是一个解决方案。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接