You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android垂直RecyclerView嵌套垂直RecyclerView滚动问题求助

解决嵌套RecyclerView子项无法滚动的问题

这个场景我太熟了!嵌套RecyclerView时,父RecyclerView会优先拦截垂直触摸事件,导致子RecyclerView根本拿不到滚动指令,下面给你两个简单有效的解决办法:

方法一:自定义子RecyclerView类

直接重写子RecyclerView的触摸事件拦截逻辑,在触摸开始时主动告诉父布局不要抢事件:

class ChildVerticalRecyclerView(context: Context, attrs: AttributeSet?) : RecyclerView(context, attrs) {
    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        when (e.action) {
            // 按下时,禁止父布局拦截触摸事件
            MotionEvent.ACTION_DOWN -> parent.requestDisallowInterceptTouchEvent(true)
            // 触摸结束或取消时,恢复父布局的拦截能力
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> parent.requestDisallowInterceptTouchEvent(false)
        }
        return super.onInterceptTouchEvent(e)
    }
}

之后把你布局文件里的子RecyclerView标签换成这个自定义类就行,比如:

<com.yourpackage.ChildVerticalRecyclerView
    android:id="@+id/childRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ... />

方法二:给子RecyclerView设置触摸监听器

不想自定义类的话,直接在代码里给子RecyclerView加个触摸监听器也能搞定:

childRecyclerView.setOnTouchListener { _, event ->
    when (event.action) {
        MotionEvent.ACTION_DOWN -> {
            // 请求父布局不要拦截当前触摸事件
            childRecyclerView.parent.requestDisallowInterceptTouchEvent(true)
        }
        MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
            // 触摸结束后恢复父布局的拦截权限
            childRecyclerView.parent.requestDisallowInterceptTouchEvent(false)
        }
    }
    // 返回false,让RecyclerView自己处理触摸事件
    false
}

额外检查点

最后再确认两个细节:

  • 子RecyclerView的layoutManager确实是垂直方向的LinearLayoutManager,比如LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
  • 子RecyclerView的高度设置为wrap_content(如果是固定高度,内容超出也不会滚动)

这样处理后,子RecyclerView就能正常响应垂直滚动了,父RecyclerView的滚动也不会受影响~

内容的提问来源于stack exchange,提问作者TopAd Studio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:24:46