RecyclerView索引溢出异常的解决方法
使用RecyclerView过程中遇到异常:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
继承并重写LinearLayoutManager.onLayoutChildren()方法
class WrappedLinearLayoutManager : LinearLayoutManager { constructor(context: Context) : super(context) constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State) { try { super.onLayoutChildren(recycler, state) } catch (e: IndexOutOfBoundsException) { e.printStackTrace() } } }
调用时使用WrappedLinearLayoutManager代替LinearLayoutManager
val recyclerAdapter = RecyclerViewAdapter(activity) val manager = WrapContentLinearLayoutManager(context).apply { orientation = LinearLayoutManager.VERTICAL } val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view).apply { layoutManager = manager adapter = recyclerAdapter }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Flutter fluro时报错type 'String' is not a subty
这篇文章主要介绍了Flutter使用fluro时报错type 'String' is not a subtype of type 'Queue<Task>'解决方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助2023-12-12Android实现评论栏随Recyclerview滑动左右移动
这篇文章主要介绍了Android实现评论栏随Recyclerview滑动左右移动效果,仿约会吧应用详情页实现,感兴趣的小伙伴们可以参考一下2016-05-05OpenHarmony实现类Android短信验证码及倒计时流程详解
这篇文章主要介绍了OpenHarmony实现类Android短信验证码及倒计时流程,发送短信验证码后,一般在界面上都会有一个倒计时的显示.在安卓中,实现类似的倒计时有多种方式,当然背后的基本原理都是设定一个初始值,然后每过一定的间隔时间执行操作2022-11-11
最新评论