美文网首页
NestedScrollView嵌套Recyclerview 瀑

NestedScrollView嵌套Recyclerview 瀑

作者: satisfying | 来源:发表于2024-11-05 11:18 被阅读0次

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

相关文章

网友评论

      本文标题:NestedScrollView嵌套Recyclerview 瀑

      本文链接:https://www.haomeiwen.com/subject/eqoadjtx.html