首页 > 系统 > Android > 正文

RecyclerView索引溢出异常的解决方法

2019-10-22 18:12:54
字体:
来源:转载
供稿:网友

使用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}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表