RecyclerView深层嵌套滑动事件解决
2022-10-30 本文已影响0人
晖仔Milo
场景
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;
}
}