Android SlidingDrawer doesn't disable buttons

2019-02-08 00:07发布

This is the scenario: I have a button B, and a slidingdrawer that when pulled out covers the entire screen. When I pull out the screen, and touch the screen where B used to be visible, its action is still executed.

How can I get around this?

I found this thread describing the very same problem, but no answer was accepted and the ones given I didn't manage to get working.

UPDATE: I have a file named Report.java, with a corresponding report.xml file as seen below.

    <SlidingDrawer
     android:id="@+id/drawer"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:handle="@+id/reportSlideButton"
     android:content="@+id/reportContent"
     android:orientation="horizontal">

    <LinearLayout 
        android:id="@id/reportContent" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_weight="1"
        android:padding="10dp"
        android:background="@color/bg_color">
            <TextView android:id="@+id/garbageTypeTextView" 
                android:layout_height="wrap_content" 
                android:textColor="@color/text" 
                android:layout_width="fill_parent" 
                android:text="@string/garbageTypeString" 
                android:textStyle="bold"/>
            <Spinner android:id="@+id/garbageTypeSpinner"
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"/>
            <TextView android:id="@+id/textViewForDateTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/dateString"
                android:textColor="@color/text" 
                android:textStyle="bold" />
            <TextView android:id="@+id/dateTextView"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textColor="@color/text" />
            <TextView android:id="@+id/textViewForAddressTitle"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content" 
                android:text="@string/addressString"
                android:textColor="@color/text" 
                android:textStyle="bold" />
            <TextView android:id="@+id/addressTextView"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textColor="@color/text" />
            <TextView  android:id="@+id/textViewForPositionTitle"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                android:text="@string/positionString"
                android:textColor="@color/text" 
                android:textStyle="bold" />
            <TextView android:id="@+id/positionTextView"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textColor="@color/text" />
            <TextView android:id="@+id/textViewForCommentTitle"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content" 
                android:text="@string/commentString"
                android:textColor="@color/text" 
                android:textStyle="bold" />
            <EditText android:id="@+id/commentTextBox"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent" 
                android:layout_weight="1"/>
            <Button android:id="@+id/sendCrapportButton" 
                android:onClick="sendCrapport"
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:text="Skicka rapport" />

    </LinearLayout>
        <Button android:id="@id/reportSlideButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:text=">"/>
</SlidingDrawer>

Adding components:

protected void addComponents() {
    takePictureButton = (ImageButton) findViewById(R.id.takePictureButton);
    slidingDrawer = (SlidingDrawer) findViewById(R.id.drawer);
}

7条回答
放荡不羁爱自由
2楼-- · 2019-02-08 00:27

you could add android:clickable="true" to your slider content tag (id reportContent). that way it won't "click through". your buttons inside the slider should still work.. i hope ;)

查看更多
该账号已被封号
3楼-- · 2019-02-08 00:29

On your SlidingDrawer, override onTouch(View v, MotionEvent event) and return true.

The one thing I am uncertain about is whether the framework will consider the drawer to overlay the View even when it is closed. If that is the case, then you should add some checks to see the state of the drawer, returning isOpened(), which will be true when the drawer is opened but false when it is closed.

查看更多
趁早两清
4楼-- · 2019-02-08 00:30

I was having the same problem. My items in the sliding drawer weren't able to gain focus. After trying several different things, I discovered that I had a placed in the sliding drawer between the tag and the Linear Layout that had the contentLayout.

Once I removed it everything work fine.

<SlidingDrawer ....>
    <FrameLayout android:id="@+id/slideHandle" ... />

        **MOVED** <ScrollView> **TO**
    <LinearLayout android:id="@+id/contentLayout" ... >
        <ScrollView> **HERE**

I hope this helps someone.

查看更多
霸刀☆藐视天下
5楼-- · 2019-02-08 00:38

I think you should add a touch listener on slider and return true on it. In that way, you will tell to the system that the touch event has been consumed.

查看更多
Juvenile、少年°
6楼-- · 2019-02-08 00:38

hey i stuck with this error for days so there is the simple answer

you've already created in your class the slidingdrawer whatever; just implement in your class , OnDrawerOpenListener,onDrawerCloseListener

then let the class add the unimplemented methods and go to the ondraweropenlistener{ slidingdrawer.setclickable(true); }

and in the drawercloselistener{ slidingdrawer.setclickable(false); }

this will set when the drawer is opened will make it clickable and prevent clicking in the behind view and when it closes every thing get back to default

this is the simplest solution try it :D

查看更多
爷的心禁止访问
7楼-- · 2019-02-08 00:39
This is My main layout and where i introduce sliding drawer inside this. 

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Beige"
    android:clickable="true"
    android:orientation="vertical" >

    <Button
    android:id="@+id/DoneStart"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:textSize="14sp" />

    <SlidingDrawer
    android:id="@+id/SlidingDrawer"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:content="@+id/contentLayout"
    android:handle="@+id/handle_image"
    android:padding="1dp"
    android:rotation="180" >

    <LinearLayout
    android:id="@+id/contentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="8"
    android:text="Hello Slider" />
    </LinearLayout>
    </SlidingDrawer>

Sample of mine after bit frustration where to add that android:clickable="true"

查看更多
登录 后发表回答