嵌套 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);
    }
}
相关推荐
恋猫de小郭11 分钟前
React 和 React Native 不再直接归属 Meta,React 基金会成立
android·前端·ios
bst@微胖子30 分钟前
鸿蒙实现滴滴出行项目之侧边抽屉栏以及权限以及搜索定位功能
android·华为·harmonyos
zcz160712782138 分钟前
Docker Compose 搭建 LNMP 环境并部署 WordPress 论坛
android·adb·docker
Pika10 小时前
深入浅出 Compose 测量机制
android·android jetpack·composer
木易 士心15 小时前
MPAndroidChart 用法解析和性能优化 - Kotlin & Java 双版本
android·java·kotlin
消失的旧时光-194315 小时前
Kotlin Flow 与“天然背压”(完整示例)
android·开发语言·kotlin
ClassOps16 小时前
Kotlin invoke 函数调用重载
android·开发语言·kotlin
努力学习的小廉16 小时前
初识MYSQL —— 数据类型
android·数据库·mysql
Lei活在当下20 小时前
【业务场景架构实战】7. 多代智能手表适配:Android APP 表盘编辑页的功能驱动设计
android·设计模式·架构
手机不死我是天子1 天前
《Android 核心组件深度系列 · 第 2 篇 Service》
android