Android SlidingDrawer 不会禁用“下方”按钮抽屉

发布于 2024-10-25 04:26:19 字数 4263 浏览 5 评论 0原文

这是场景: 我有一个按钮 B 和一个滑动抽屉,拉出时会覆盖整个屏幕。 当我拉出屏幕并触摸 B 原来可见的屏幕时,它的动作仍然被执行。

我该如何解决这个问题?

我发现这个线程描述了非常同样的问题,但没有答案被接受,而且我给出的答案也无法开始工作。

更新:我有一个名为 Report.java 的文件,以及相应的 report.xml 文件,如下所示。

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

添加组件:

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

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

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

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

发布评论

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

评论(7

暗喜 2024-11-01 04:26:19

您可以将 android:clickable="true" 添加到滑块内容标签(id reportContent)。这样它就不会“点击通过”。你滑块内的按钮应该仍然可以工作..我希望;)

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

旧伤还要旧人安 2024-11-01 04:26:19

只需添加 @f-horn 的答案:

如果您为 SlidingDrawer 包含来自不同文件的布局(就像我一样),则必须将 'android:clickable="true"' 放在包含的布局文件中,而不是在包含标签中。让我举个例子:

这行不通:

main.xml

<SlidingDrawer  android:handle="@+id/handle"
       android:content="@+id/content">

        <ImageView android:id="@id/handle" />
        <include android:id="@+id/content" layout="@layout/some_other_layout" 
        android:clickable="true"/>
</SlidingDrawer>'

这会:

main.xml

<SlidingDrawer  android:handle="@+id/handle"
        android:content="@+id/content">

        <ImageView android:id="@id/handle" />
        <include android:id="@+id/content" layout="@layout/some_other_layout"/>
</SlidingDrawer>'

some_other_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:clickable="true"> ............
<LinearLayout/>

Just adding to @f-horn 's answer:

If you include a layout from a different file (like I do) for the SlidingDrawer, you have to put the 'android:clickable="true"' in the included layout file, not in the include tag. Let me rather use an example:

This will not work:

main.xml

<SlidingDrawer  android:handle="@+id/handle"
       android:content="@+id/content">

        <ImageView android:id="@id/handle" />
        <include android:id="@+id/content" layout="@layout/some_other_layout" 
        android:clickable="true"/>
</SlidingDrawer>'

This will:

main.xml

<SlidingDrawer  android:handle="@+id/handle"
        android:content="@+id/content">

        <ImageView android:id="@id/handle" />
        <include android:id="@+id/content" layout="@layout/some_other_layout"/>
</SlidingDrawer>'

some_other_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:clickable="true"> ............
<LinearLayout/>
顾铮苏瑾 2024-11-01 04:26:19

我认为你应该在滑块上添加一个触摸监听器并在其上返回 true 。
这样,您将告诉系统触摸事件已被消耗。

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.

两个我 2024-11-01 04:26:19

嘿,我已经坚持这个错误好几天了,所以

你已经在课堂上创建了一个简单的答案:滑动抽屉等等;
只需在您的类中实现 OnDrawerOpenListener、onDrawerCloseListener

然后让该类添加未实现的方法并转到 ondraweropenlistener{
slipdrawer.setclickable(true);
}

drawercloselistener{
slipdrawer.setclickable(false);
}

这将在抽屉打开时设置,使其可点击并防止在后面的视图中点击,当它关闭时,所有内容都会恢复默认状态,

这是最简单的解决方案,请尝试一下:D

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

妞丶爷亲个 2024-11-01 04:26:19

我也遇到了同样的问题。我的滑动抽屉中的物品无法获得焦点。在尝试了几种不同的方法之后,我发现我在标签和具有 contentLayout 的线性布局之间放置了一个滑动抽屉。

一旦我删除它,一切就正常了。

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

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

我希望这对某人有帮助。

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.

残疾 2024-11-01 04:26:19

在你的 SlidingDrawer 上,重写 onTouch(View v, MotionEvent event) 并返回 true。

我不确定的一件事是,即使抽屉关闭,框架是否也会考虑抽屉覆盖视图。如果是这种情况,那么您应该添加一些检查来查看抽屉的状态,返回 isOpened(),当抽屉打开时返回 true,关闭抽屉时返回 false。

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.

荭秂 2024-11-01 04:26:19
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>

我的示例在有点沮丧之后在哪里添加 android:clickable="true"

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"

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