嵌套 RecyclerView 的事件处理方法

在项目开发的过程中,可能会遇到需要处理的 RecyclerView 嵌套的复杂需求,这里提供一个比较简单的实现模板,假如在 RecyclerView 里嵌套了一个 RecyclerView,在里面能滑动的同时,也可以在滑动到顶/底的时候,拖动父 RecyclerView

java 复制代码
package com.atoto.module.main.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


/**
 * 描述: 技能子项:处理 RecyclerView 嵌套滚动问题
 * 创建者: IT乐手
 * 日期: 2025/3/31
 */

public class SubRecyclerView extends RecyclerView {

    private float mLastY = 0f;

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

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

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

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                boolean canUpScroll = canScrollVertically(1); //能否向上滑动
                boolean canDownScroll = canScrollVertically(-1); //能否向下滑动
                if (canUpScroll || canDownScroll) {//可见,并且能滑动,请求父Rv不拦截事件
                    getParent().requestDisallowInterceptTouchEvent(true);
                    mLastY = e.getY();
                }
            }
            break;
            case MotionEvent.ACTION_MOVE: {
                float diff = mLastY - e.getY();
                mLastY = e.getY();
                if (diff >= 0) {//手指向上滑动
                    if (canScrollVertically(1)) {//子Rv未滑到底部,请求父Rv不拦截事件
                        getParent().requestDisallowInterceptTouchEvent(true);
                    } else {//子Rv滑到底部了,父Rv可以拦截事件                        
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                } else {//手指向下滑动
                    if (canScrollVertically(-1)) {//子Rv未滑到顶部,请求父Rv不拦截事件
                        getParent().requestDisallowInterceptTouchEvent(true);
                    } else {//子Rv滑到顶部了,父Rv可以拦截事件                        
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
            }
            break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                getParent().requestDisallowInterceptTouchEvent(false);
                mLastY = 0f;
            }
            break;
        }
        return super.dispatchTouchEvent(e);
    }
}
相关推荐
2501_916007473 小时前
从零开始学习iOS App开发:Xcode、Swift和发布到App Store完整教程
android·学习·ios·小程序·uni-app·iphone·xcode
姝然_95273 小时前
ConstraintLayout属性详解
android
2501_916008894 小时前
前端工具全景实战指南,从开发到调试的效率闭环
android·前端·小程序·https·uni-app·iphone·webview
浅影歌年5 小时前
Android和h5页面相互传参
android
用户69371750013845 小时前
搞懂 Kotlin 软关键字与硬关键字:灵活命名与语法陷阱全解析
android
下位子6 小时前
『OpenGL学习滤镜相机』- Day2: 渲染第一个三角形
android·opengl
风语者日志7 小时前
[LitCTF 2023]这是什么?SQL !注一下 !
android·数据库·sql
2501_915921437 小时前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹7 小时前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友
游戏开发爱好者88 小时前
iOS 应用逆向对抗手段,多工具组合实战(iOS 逆向防护/IPA 混淆/无源码加固/Ipa Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview