NestedScrollView 中自定义View中存在Recyclerview 且layoutManager 是StaggeredGridLayoutManager 时,view 高度为0 不正常显示数据
解决
class CustomRecyclerView(context: Context, attrs: AttributeSet) : RecyclerView(context, attrs) {
override fun onMeasure(widthSpec: Int, heightSpec: Int) {
// 设置一个较大的高度测量值,避免被压缩
val expandSpec = MeasureSpec.makeMeasureSpec(Int.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
super.onMeasure(widthSpec, expandSpec)
}
}
2.替换Recyclerview 并禁止滑动
<com.example.CustomRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"/>
val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
recyclerView.layoutManager = staggeredGridLayoutManager