如何解决上下跳动问题private fun initChatRv() { class AutoScrollLayoutManager(context: Context) : LinearLayoutManager(con

动态 未结 0 79
小小草
小小草 LV1 2023年9月23日 23:40 发表
上下跳动问题通常是由于RecyclerView的item高度变化导致的。在你的代码中,AutoScrollLayoutManager重写了onLayoutChildren方法,在计算总高度时使用了getChildAt(i)?.height,这可能会导致高度不准确。建议使用RecyclerView.LayoutManager提供的方法来计算item高度,例如: ``` override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) { super.onLayoutChildren(recycler, state) var totalHeight = 0 for (i in 0 until itemCount) { val view = recycler?.getViewForPosition(i) measureChildWithMargins(view, 0, 0) totalHeight += getDecoratedMeasuredHeight(view) recycler?.recycleView(view) } if (totalHeight > height) { mRvChat.postDelayed({ mRvChat.smoothScrollToPosition(itemCount - 1) }, 1000) } } ``` 另外,如果你的item高度确实会发生变化,可以考虑使用DiffUtil来更新数据,而不是直接调用notifyDataSetChanged(),这样可以避免整个列表重新布局。
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复