嵌套 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);
    }
}
相关推荐
百***61871 小时前
Spring的构造注入
android·java·spring
Tom4i1 小时前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin
yi诺千金2 小时前
Android U 自由窗口(浮窗)——启动流程(system_server侧流程)
android
清空mega4 小时前
第11章 网络编程
android·网络
自动化BUG制造器4 小时前
Android UI 线程不一定是主线程
android
无知的前端5 小时前
一文读懂-Jetpack与AndroidX
android·kotlin·android jetpack
河铃旅鹿6 小时前
Android开发-java版:SQLite数据库
android·数据库·笔记·学习·sqlite
旋律逍遥6 小时前
《Framework 开发》3、开发工具及命令行知识装备
android
啦啦9117146 小时前
安卓手机/平板/TV版 Rotation强制横屏显示工具!免ROOT可用!再推荐突破手机限制的3款神器
android·智能手机·电脑
汤面不加鱼丸7 小时前
flutter实践:混合app在部分android旧机型上显示异常
android·flutter