RecyclerView触摸事件拦截

        mBinding.rvSrt.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 SrtVH) {
                            rv.requestDisallowInterceptTouchEvent(((SrtVH) viewHolder.baseViewHolder).isTouchNsv(e.getRawX(), e.getRawY())
                                    || ((SrtVH) viewHolder.baseViewHolder).isTouchSrtNsv(e.getRawX(), e.getRawY()));

                            //拦截MotionEvent.ACTION_UP,移动到用户最后触摸到的VH
                            if (e.getAction() == MotionEvent.ACTION_UP && !mDubViewModel.checkBoolean(mDubViewModel.isRecording)) {
                                SrtVH srtVH = ((SrtVH) viewHolder.baseViewHolder);
                                if (srtVH.getPosition() != ((LinearLayoutManager) mBinding.rvSrt.getLayoutManager()).findFirstVisibleItemPosition() - mSrtAdapter.getHeaderSize()) {
                                    FZLogger.d(TAG, "即将移动到:" + srtVH.getPosition());
                                    int[] distanceToFinalSnap = mPagerSnapHelper.calculateDistanceToFinalSnap(mBinding.rvSrt.getLayoutManager(), srtVH.getItemView());
                                    if (distanceToFinalSnap != null) {
                                        mBinding.rvSrt.smoothScrollBy(0, distanceToFinalSnap[1]);
                                    }
                                }
                            }
                        }
                    }
                }
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
    public boolean isTouchSrtNsv(float x, float y) {
        if (mBinding.scrollView.isOverMaxHeight()) {
            int[] pos = new int[2];
            mBinding.scrollView.getLocationOnScreen(pos);
            int width = mBinding.scrollView.getMeasuredWidth();
            int height = mBinding.scrollView.getMeasuredHeight();
            return x >= pos[0] && x <= pos[0] + width && y >= pos[1] && y <= pos[1] + height;
        } else {
            return false;
        }
    }
public class MaxHeightScrollView extends ScrollView {

    private int mMaxHeight;

    public MaxHeightScrollView(@NonNull Context context) {
        super(context);
    }

    public MaxHeightScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MaxHeightScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMaxHeight(int maxHeight) {
        mMaxHeight = maxHeight;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int maxHeight;
        if (mMaxHeight == 0) {
            maxHeight = FZUtils.dp2px(getContext(), 208);
        } else {
            maxHeight = mMaxHeight;
        }
        if (getMeasuredHeight() > maxHeight) {
            setMeasuredDimension(getMeasuredWidth(), maxHeight);
        }
    }

    public boolean isOverMaxHeight() {
        return getMeasuredHeight() >= mMaxHeight;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容