嵌套 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);
    }
}
相关推荐
故渊at22 分钟前
第二板块:Android 四大组件标准化学理 | 第六篇:四大组件架构总论与 Manifest 规范
android·架构·zygote·manifest·四大组件
Jinkxs1 小时前
Python基础 - 文件的写入操作 write与writelines方法
android·服务器·python
jason.zeng@15022071 小时前
(第二讲)Android开发取摄像头流的基础(ImageAnalysis)
android
敲代码的瓦龙2 小时前
操作系统?Android与Linux!!!
android·linux·运维
愚公搬代码2 小时前
【愚公系列】《移动端AI应用开发》017-Android端应用开发(网络通信与API集成)
android·人工智能
say_fall3 小时前
可编程中断控制器8259A工作方式超详细解析
android·开发语言·学习·硬件架构·硬件工程
甜瓜看代码4 小时前
SystemUI 启动与组成机制
android·源码·源码阅读
黄林晴5 小时前
Kotlin 2.4.0 正式稳定!Android 升级、Compose、KMP 全变化详解
android·kotlin
恋猫de小郭6 小时前
Android 官方给 Compose 搞了个不需要 UI 环境的 Composable
android·前端·flutter
珊瑚里的鱼7 小时前
C++的强制类型转换
android·开发语言·c++