Kotlin中ScrollView中嵌套RecyclerView

1.ScrollView中嵌套RecyclerView且支持上下滑动,并且不影响ScrollView

XML 复制代码
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
		<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:padding="5dp"
            android:scrollbars="vertical"
            android:nestedScrollingEnabled="true"/><!--nestedScrollingEnabled这句重要 -->
</androidx.core.widget.NestedScrollView>

2.ScrollView中嵌套RecyclerView,RecyclerView列表全部展开不能滑动,焦点在ScrollView可上下滑动

自定义view

Kotlin 复制代码
package com.ui.view

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.RecyclerView

class ScrollRecyclerView(context: Context, attrs: AttributeSet?) : RecyclerView(context, attrs) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val heightMeasureSpecCustom = MeasureSpec.makeMeasureSpec(
            Int.MAX_VALUE shr 2, MeasureSpec.AT_MOST
        )
        super.onMeasure(widthMeasureSpec, heightMeasureSpecCustom)
        val params = layoutParams
        params.height = measuredHeight
    }
}

使用

XML 复制代码
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
		<com.ui.view.ScrollRecyclerView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:padding="5dp"
            android:scrollbars="vertical"/>
</ScrollView>