gpt4 book ai didi

android - Android SlidingDrawer 中的组合处理程序

转载 作者:行者123 更新时间:2023-11-30 04:10:01 29 4
gpt4 key购买 nike

我的问题如下:我想创建一个滑动抽屉,其中处理程序(滑动用于打开/关闭的 View )是一个组合 View ,其方式如下图所示:

enter image description here

期望的行为是:

  1. 当用户点击处理程序时,抽屉打开(按钮跟随 View ,因为它附加在它上面);
  2. 如果我点击按钮,系统会做出一些不同的事情(在我的例子中,我会打开一个对话框,其中包含将 View 添加为抽屉的子项的选项);

这种实现的主要问题是监听器发生冲突,因为按钮“+”是处理程序的一部分并且无法创建重写的监听器 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;
}
}
}
}

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

关于android - Android SlidingDrawer 中的组合处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11005970/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com