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>