How can I disable a view behind my SlidingDrawer in Android?

风格不统一 提交于 2019-11-29 16:58:35

问题


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?


回答1:


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.




回答2:


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>



回答3:


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!




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/2596571/how-can-i-disable-a-view-behind-my-slidingdrawer-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!