我的问题如下:我想创建一个滑动抽屉,其中处理程序(滑动用来打开/关闭的视图)是一个组合视图,其方式是像下面的按钮那样存在一个按钮:



所需的行为是:


当用户单击处理程序时,抽屉将打开(按钮随视图附在视图上)。
如果单击该按钮,则系统会有所不同(以我为例,我打开一个对话框,其中包含将视图添加为抽屉子视图的选项);


这种实现方式的主要问题是,由于按钮“ +”是处理程序的一部分,并且无法创建覆盖的监听器onclick,因此监听器发生冲突。作为第一种方法,我正在考虑以编程方式完成所有这些操作,但是我真的很想知道是否存在另一种简单的方法来进行此布局。

有没有人提供提示或只知道使用xml实现此方法?
提前致谢!

最佳答案

您可以将LinearLayout用作SlidingDrawer容器的句柄或内容元素的容器。如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <SlidingDrawer android:id="@+id/drawer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="showPopUp"
        android:handle="@+id/handle"
        android:content="@+id/content">
        <LinearLayout android:id="@id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- here goes your content -->
        </LinearLayout>
        <LinearLayout android:id="@id/handle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffff5600"
            android:orientation="horizontal">
            <TextView android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:layout_gravity="left"
                android:text="musicas" />
            <Button android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="showPopUp"
                android:text="+" />
        </LinearLayout>
    </SlidingDrawer>
</LinearLayout>


尽管它不允许捕获按钮按下事件,但它允许您像上面的屏幕快照那样进行布局。为此,我进行了一些研究,并建议重写MultiDirectionSlidingDrawer(源http://blog.sephiroth.it/2011/03/29/widget-slidingdrawer-top-to-bottom/)。
我这样做如下:
在onInterceptTouchEvent()方法中,我在final View handle = mHandle;之后添加了以下代码

    boolean handleTouch = false;
    if (mHandle instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) mHandle;

        int count = group.getChildCount();
        for (int i = 0; i < count; i++) {
            View v = group.getChildAt(i);
            v.getHitRect(frame);
            if (frame.contains((int) x, (int) y)) {
                handleTouch = v.onTouchEvent(event);
                if (handleTouch) {
                    return false;
                }
            }
        }
    }


现在,它分别处理按钮和抽屉本身的事件。

10-07 19:14
查看更多