解决 RecycleView里面的 RecycleView 不好滑的问题

7 阅读1分钟

 完全禁用外层 RecyclerView 的滑动功能(核心实现)

// 自定义不可滑动的 RecyclerView
class NonScrollRecyclerView(
    context: Context, 
    attrs: AttributeSet? = null
) : RecyclerView(context, attrs) {

    // 禁用触摸事件拦截
    override fun onInterceptTouchEvent(e: MotionEvent): Boolean = false
    
    // 禁用自身滚动
    override fun onTouchEvent(e: MotionEvent): Boolean = false
    
    // 强制不处理滚动
    override fun dispatchNestedPreScroll(
        dx: Int,
        dy: Int,
        consumed: IntArray?,
        offsetInWindow: IntArray?,
        type: Int
    ): Boolean = false
}

<!-- 外层 RecyclerView -->
<com.example.NonScrollRecyclerView
    android:id="@+id/outer_recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never"
    android:scrollbars="none"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

<!-- 内层可滑动 RecyclerViewItem 布局 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"> <!-- 固定高度 -->

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/inner_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"
        android:orientation="horizontal"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>

动态配置优化