RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position
2016-12-12 18:58
459 查看
使用RecycleView的时候报RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position,搜索引擎找到如下blog:
http://www.mamicode.com/info-detail-1436789.html
PS:亲测有效,没有效果的同学请仔细理清OntouchListener的逻辑
今早我更新了 Android Studio 1.2 Preview 和 22 版本的 sdk,包括兼容库也更新到 22.0.0,但是令人失望的是,RecyclerView 存在的一个明显的 bug 仍然没有修复:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position…
重现的方法是:使用 RecyclerView 加官方下拉刷新的时候,如果绑定的 List 对象在更新数据之前进行了 clear,而这时用户紧接着迅速上滑 RV,就会造成崩溃,而且异常不会报到你的代码上,属于RV内部错误。初次猜测是,当你 clear 了 list 之后,这时迅速上滑,而新数据还没到来,导致 RV 要更新加载下面的 Item 时候,找不到数据源了,造成 crash.
但明显,更新数据之前 clear list 是挺常见的做法,你不可能祈祷用户这时候乖乖不动等待新数据加载完,所以根本就是不合理的。
嗯,Google 有时真是令人非常失望。好在我想到了解决这个问题的方法。
就是在刷新,也就是 clear 的同时,让 RecyclerView 暂时不能够滑动,之后再允许滑动即可。代码就是在 RecyclerView 初始化的时候加上是否在刷新进而拦截手势:
然后去改变和恢复 mIsRefreshing 这个 boolean 即可。想象中觉得如果不让用户滑动很愚蠢,但不得不这么做的时候,发现真实效果很算完美,很不错的!我想基本上使用 RecyclerView 都会遇到这个问题,所以将它整理发表出来,一起绕过这个坑吧。
http://www.mamicode.com/info-detail-1436789.html
PS:亲测有效,没有效果的同学请仔细理清OntouchListener的逻辑
今早我更新了 Android Studio 1.2 Preview 和 22 版本的 sdk,包括兼容库也更新到 22.0.0,但是令人失望的是,RecyclerView 存在的一个明显的 bug 仍然没有修复:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position…
重现的方法是:使用 RecyclerView 加官方下拉刷新的时候,如果绑定的 List 对象在更新数据之前进行了 clear,而这时用户紧接着迅速上滑 RV,就会造成崩溃,而且异常不会报到你的代码上,属于RV内部错误。初次猜测是,当你 clear 了 list 之后,这时迅速上滑,而新数据还没到来,导致 RV 要更新加载下面的 Item 时候,找不到数据源了,造成 crash.
但明显,更新数据之前 clear list 是挺常见的做法,你不可能祈祷用户这时候乖乖不动等待新数据加载完,所以根本就是不合理的。
Stack Overflow 上也有这个问题,未能够解决: How to change contents of RecyclerView while scrolling Google code 论坛上也有这个 issue,一堆跟帖,都是描述如何重现,未能够解决: https://code.google.com/p/android/issues/detail?id=77846
嗯,Google 有时真是令人非常失望。好在我想到了解决这个问题的方法。
就是在刷新,也就是 clear 的同时,让 RecyclerView 暂时不能够滑动,之后再允许滑动即可。代码就是在 RecyclerView 初始化的时候加上是否在刷新进而拦截手势:
mRecyclerView.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (mIsRefreshing) { return true; } else { return false; } } } );
然后去改变和恢复 mIsRefreshing 这个 boolean 即可。想象中觉得如果不让用户滑动很愚蠢,但不得不这么做的时候,发现真实效果很算完美,很不错的!我想基本上使用 RecyclerView 都会遇到这个问题,所以将它整理发表出来,一起绕过这个坑吧。
相关文章推荐
- 成功解决RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- 【转载】Android RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position 解决方案
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position …
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- RecyclerView内部BUG: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item positio
- 滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案
- Android中IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:4
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
- IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter notifyItemRangeRemove
- IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
- IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state