java.lang.IndexOutOfBoundsException Inconsistency detected. Invalid view holder adapter position...

先上崩溃日志。
java.lang.IndexOutOfBoundsException

Inconsistency detected. Invalid view holder adapter positionViewHolder{762658d position=104 id=-1, oldPos=-1, pLpos:-1 no parent} com.hpplay.picturelib.widgets.RecyclerPreloadView{7e1b40a VFED..... ......I. 0,0-1080,1795 #7f0b046e app:id/picture_recycler}, adapter:com.hpplay.happycast.externalscreen.adapter.PictureImageGridAdapter@9b0e47b, layout:com.hpplay.view.utils.MyGridLayoutManager@810f298, context:com.hpplay.happycast.externalscreen.photo.PictureExternalScreenSelectorActivity@8c3297e

莫名其妙的崩溃,上百度,度出来的全是垃圾,什么自定义LinearLayoutManager,对onLayoutChildren try catch啦。大爷的,误导人。

@Override

public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

try {

super.onLayoutChildren(recycler, state);

}catch (IndexOutOfBoundsException e) {

LePlayLog.w(TAG, e);

}}


首先,这个问题的根本原因是RecyclerView在滑动的过程是不能更新数据源的。具体为什么,我也不清楚,有人说是android系统bug,我们就认为它是系统bug吧,反正在RecyclerView在滚动时无论是notifyDataSetChanged还是notifyItemRangeChanged都是会崩溃的。

找了各种办法,验证都是崩溃。。。

最后,上大招:

不是滑动更新就崩溃吗?我不让它滑动不行了?

在滑动时需要更新数据源的前面写上这么一句:

if (mRecyclerView.getScrollState() == RecyclerView.SCROLL_STATE_SETTLING) {

mRecyclerView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL,0,0,0));

}

RecyclerView滑动继续,更新数据也没有崩溃。完美修复问题!!!

你可能感兴趣的:(java.lang.IndexOutOfBoundsException Inconsistency detected. Invalid view holder adapter position...)