android - 将按钮添加到 SlidingDrawer 句柄?

标签 android onclick slidingdrawer

我已经研究了一段时间。这个想法开始很简单,我想要一个 SlidingDrawer handle 上的按钮,以允许用户查看特定于抽屉内容的设置。所以我做了一个布局,旁边有一个按钮,并将其设置为 handle 。抽屉画得很好,但不允许按下按钮(在 Handlebars 上)。每当我尝试点击这个东西时,点击都会被解释为 handle 点击,并切换抽屉的状态。

有人知道这是怎么回事吗?

谢谢~Aedon

最佳答案

我会发布我的实现,以免其他人遇到麻烦。

基本上,您必须扩展 SlidingDrawer 类并处理 onInterceptTouch 事件,以便当它们位于句柄布局内的项目顶部时通过。

这假设您正在为 handle 使用 ViewGroup(例如任何布局),并且其中的所有 View 都是可点击的。

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SlidingDrawer;

public class ClickableSlidingDrawer extends SlidingDrawer {

    private ViewGroup mHandleLayout;
    private final Rect mHitRect = new Rect();

    public ClickableSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ClickableSlidingDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View handle = getHandle();

        if (handle instanceof ViewGroup) {
            mHandleLayout = (ViewGroup) handle;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (mHandleLayout != null) {
            int childCount = mHandleLayout.getChildCount();
            int handleClickX = (int)(event.getX() - mHandleLayout.getX());
            int handleClickY = (int)(event.getY() - mHandleLayout.getY());

            Rect hitRect = mHitRect;

            for (int i=0;i<childCount;i++) {
                View childView = mHandleLayout.getChildAt(i);
                childView.getHitRect(hitRect);

                if (hitRect.contains(handleClickX, handleClickY)) {
                    return false;
                }
            }
        }

        return super.onInterceptTouchEvent(event);
    }
}

然后,在您的布局 .xml 中使用 <my.package.name.ClickableSlidingDrawer>而不是 <SlidingDrawer>

关于android - 将按钮添加到 SlidingDrawer 句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666693/

相关文章:

c++ - 使用 Qt 修改任何复选框状态时执行操作

javascript - 无法单击 onClick() 事件下拉列表中的 anchor 标记

java - 从 ListView 中删除所选项目不起作用

android - 在带有 SlidingDrawer 的图形布局中时 Eclpse 3.5 和 3.6 中的 ClassCastException

android - GoogleMap 上的滑动抽屉

android - 无法使用 Google drive api android 从 google drive 查询文件 "shared with me"

android - Top_toBottomof 在 constraintlayout 中不起作用。如何在Constraint布局中使用Top_toBottomof?

java - 读取 Android POST 请求的回显

android - 视频作为启动画面而不是图片

android - 如何在 Android 中禁用 SlidingDrawer 后面的 View ?