adapter更新导致的IndexOutOfBoundsException

1、崩溃日志

 java.lang.IndexOutOfBoundsException:
        at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition (RecyclerView.java:5974)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6158)
        at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline (GapWorker.java:288)
        at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline (GapWorker.java:345)
        at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline (GapWorker.java:361)
        at androidx.recyclerview.widget.GapWorker.prefetch (GapWorker.java:368)
        at androidx.recyclerview.widget.GapWorker.run (GapWorker.java:399)
        at android.os.Handler.handleCallback (Handler.java:938)
        at android.os.Handler.dispatchMessage (Handler.java:99)
        at android.os.Looper.loop (Looper.java:246)
        at android.app.ActivityThread.main (ActivityThread.java:8653)
        at java.lang.reflect.Method.invoke (Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)

2、原因分析:

  videoFileDataDetailList.clear()
  videoFileDataDetailList.addAll(itemData.videoFileDataList)
  adapterDetail?.notifyItemRangeChanged(0, videoFileDataDetailList.size)
 对该外部数据集做了两个操作:先移除数据,然后添加数据,之后notify数据集。这里,添加数数据时     (Adapter的  内部数据集内容还处在外部数据集移除数据之前),造成了内部和外部数据集不一致

3、解决方法:

      adapter?.notifyDataSetChanged()

https://www.jianshu.com/p/2eca433869e9

你可能感兴趣的:(adapter更新导致的IndexOutOfBoundsException)