完全禁用外层 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"/>
<!-- 内层可滑动 RecyclerView 的 Item 布局 -->
<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>
动态配置优化