美文网首页
RecyclerView深层嵌套滑动事件解决

RecyclerView深层嵌套滑动事件解决

作者: 晖仔Milo | 来源:发表于2022-10-30 21:55 被阅读0次

    场景
    1、RecyclerView嵌套了别的ViewGroup,ViewGroup中又嵌套了类似于RecyclerView、NestedScrollView之类的view;

    原理
    在 RecycleView.addOnItemTouchListener >> 拦截事件,在需要滑动的子View中消费掉

    解决:

            binding.recycleView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
                @Override
                public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                    View view = rv.findChildViewUnder(e.getX(), e.getY());
                    if (view != null) {
                        RecyclerView.ViewHolder holder = rv.getChildViewHolder(view);
                        if (holder instanceof CommonRecyclerAdapter.ViewHolder) {
                            CommonRecyclerAdapter.ViewHolder viewHolder = (CommonRecyclerAdapter.ViewHolder) holder;
                            if (viewHolder.baseViewHolder instanceof HomeTabPlanEmptyVH) {
                                rv.requestDisallowInterceptTouchEvent(((HomeTabPlanEmptyVH) viewHolder.baseViewHolder).isTouchNsv(e.getRawX(), e.getRawY()));
                            }
                        }
                    }
                    return false;
                }
    
                @Override
                public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
    
                }
    
                @Override
                public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    
                }
            });
    
        public boolean isTouchNsv(float x, float y) {
            if (mBinding.layoutList.getVisibility() == View.VISIBLE) {
                int[] pos = new int[2];
                mBinding.layoutList.getLocationOnScreen(pos);
                int width = mBinding.layoutList.getMeasuredWidth();
                int height = mBinding.layoutList.getMeasuredHeight();
                return x >= pos[0] && x <= pos[0] + width && y >= pos[1] && y <= pos[1] + height;
            } else {
                return false;
            }
        }
    

    相关文章

      网友评论

          本文标题:RecyclerView深层嵌套滑动事件解决

          本文链接:https://www.haomeiwen.com/subject/cqwltdtx.html