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

 更新时间:2018年04月17日 09:22:18   作者:phantomvk  
本篇文章主要介绍了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
}

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

相关文章

  • OpenGL ES纹理详解

    OpenGL ES纹理详解

    这篇文章主要为大家详细介绍了OpenGL ES纹理的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android App中实现相册瀑布流展示的实例分享

    Android App中实现相册瀑布流展示的实例分享

    这篇文章主要介绍了Android App中实现相册瀑布流展示的实例分享,例子中利用到了缓存LruCache类的相关算法来解决大量加载问题,需要的朋友可以参考下
    2016-04-04
  • Android实现点赞动画(27)

    Android实现点赞动画(27)

    这篇文章主要为大家详细介绍了Android实现点赞动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android Studio 配置忽略文件的方法实现

    Android Studio 配置忽略文件的方法实现

    这篇文章主要介绍了Android Studio 配置忽略文件的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 学习Android自定义Spinner适配器

    学习Android自定义Spinner适配器

    这篇文章主要为大家详细介绍了学习Android自定义Spinner适配器的相关资料,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android自定义ViewGroup实现右滑进入详情

    Android自定义ViewGroup实现右滑进入详情

    这篇文章主要为大家详细介绍了Android如何通过自定义ViewGroup实现右滑进入详情效果,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-01-01
  • android dialog边框去除白色边框实现思路及代码

    android dialog边框去除白色边框实现思路及代码

    android dialog边框含有白色真是美中不足啊,本文将介绍如何去除白色边框,有思路及代码,感兴趣的朋友可以了解下
    2013-01-01
  • Android WebView调用本地相册的方法

    Android WebView调用本地相册的方法

    这篇文章主要为大家详细介绍了Android WebView调用本地相册的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android第三方登录之腾讯QQ登录的实例代码

    Android第三方登录之腾讯QQ登录的实例代码

    本文通过实例代码给大家接收了Android第三方登录之腾讯QQ登录的实现方法,非常不错,具有参考借鉴价值,需要的的朋友参考下吧
    2017-06-06
  • Android实现换肤的两种思路分析

    Android实现换肤的两种思路分析

    这篇文章主要介绍了Android实现换肤的两种思路分析,较为详细的分析了Android实现换肤的具体方法,需要的朋友可以参考下
    2015-12-12

最新评论