Gavin-ZYX / StickyDecoration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

悬浮view点击事件会穿透到下层item的点击事件,导致悬浮点击事件无效

xhh1993 opened this issue · comments

commented

悬浮view点击事件会穿透到下层item的点击事件,导致悬浮点击事件无效

由于点击事件是用过setOnTouchListener来实现的,在点击顶部悬浮区域的时候,这个事件被分发到子View里面去了,于是这个事件无法监听到。

在Decoration上,暂时没有想到好的处理方式。
临时的解决方案:
方案一:处理RecyclerView的事件分发,对顶部这个区域的事件单独处理;
方案二:在顶部覆盖一个透明的View,来拦截这个点击事件;

commented

我尝试使用recyclerview的addOnItemTouchListener去拦截item的onClick方法,可以拦截,但悬浮view的坐标貌似有问题。希望你可以尝试一下,

我尝试了这两种解决方式,还是不行

穿透问题,在RecyclerView中拦截事件即可。如

public class MyRecyclerView extends RecyclerView {

    private BaseDecoration mDecoration;

    @Override
    public void addItemDecoration(ItemDecoration decor) {
        if (decor != null && decor instanceof BaseDecoration) {
            mDecoration = (BaseDecoration) decor;
        }
        super.addItemDecoration(decor);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (mDecoration != null) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mDecoration.onEventDown(e);
                    break;
                case MotionEvent.ACTION_UP:
                    if (mDecoration.onEventUp(e)) {
                        return true;
                    }
                    break;
                default:
            }
        }
        return super.onInterceptTouchEvent(e);
    }
}

(onEventDown、onEventUp在1.4.8中添加)