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

发布于 2024-08-27 16:55:31 字数 247 浏览 9 评论 0原文

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

帮助?

I have a SlidingDrawer that pops up from the bottom of the screen and fills the screen about 80%. Even though the SlidingDrawer view is in focus, it is still possible to click on items, buttons and other elements in the view that is behind the SlidingDrawer. When SlidingDrawer is active/pulled up/in focus, I want to disable the entire view behind it so it will not be able to recieve clicks and touches. Is there a good way to disable an entire view? I have tried setEnable(false) and setClickable(false) but neither of them work.

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

看海 2024-09-03 16:55:31

这是解决这个问题的方法(我也需要一个解决方案) - 获取保存内容的 LinearLayout 并添加一个单击侦听器。让点击侦听器响应点击(扔掉,无论如何) - 然后这会阻止它传播到滑动抽屉下方的视图 - 它对我有用 - 并且它不会阻塞抽屉中的其他项目。

Here's a way to get around this problem (I needed a solution also) - grab the linearLayout that holds the contents and add a click listener. Have the click listener respond to clicks (throw away, whatever) - and this then stops it propagating to the view below the sliding drawer - it works for me - and it doesn't block the other items in the drawer.

把人绕傻吧 2024-09-03 16:55:31

在侧面布局中执行 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>

In side Layout do android:clickable="true"

For Example Following file is drawer.xml

Inside 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>
傻比既视感 2024-09-03 16:55:31

乔的回答对我没有帮助。我的情况有点不同。我有一个有两个孩子的 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);
        }
    }
}

享受吧!

Joe's answer did not do the trick for me. My scenario is a bit diferent. I have a FrameLayout with two children. Only one of the children has to be 'active' at a given moment, and while the second is active the first should no longer process any input. My solution:

    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);
        }
    }
}

Enjoy!

空城缀染半城烟沙 2024-09-03 16:55:31

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

所以我认为这可能是一个解决方案。

I've tried to put SlidingDrawer in the RelativeLayout, instead of LinearLayout. And set
mySlidingDrawer.bringToFront() in opening method.

So I think this may be a solution.

染柒℉ 2024-09-03 16:55:31

I don't know if this will work, but it's worth a try. Call
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS) on your container View (the one that the sliding drawer slides over).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文