滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30

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 初始化的时候加上是否在刷新进而拦截手势:


1

2

3

4

5

6

7

8

9

10

11

12

mRecyclerView.setOnTouchListener(

        new View.OnTouchListener() {

            @Override

            public boolean onTouch(View v, MotionEvent event) {

                if (mIsRefreshing) {

                    return true;

                } else {

                    return false;

                }

            }

        }

);

然后去改变和恢复 mIsRefreshing 这个 boolean 即可。想象中觉得如果不让用户滑动很愚蠢,但不得不这么做的时候,发现真实效果很算完美,很不错的!我想基本上使用 RecyclerView 都会遇到这个问题,所以将它整理发表出来,一起绕过这个坑吧。

时间: 2024-07-31 05:28:18

滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30的相关文章

【转载】Android RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position

转载自:Drakeet的个人博客 RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position 今早我更新了 Android Studio 1.2 Preview 和 22 版本的 sdk,包括兼容库也更新到 22.0.0,但是令人失望的是,RecyclerView 存在的一个明显的 bug 仍然没有修复: java.lang.IndexOutOfBoundsException:

RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.

今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency detected.   Invalid view holder adapter positionViewHolder{56798b2 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent} 在网

java.lang.IndexOutOfBoundsException: Inconsistency detected.

1 这个bug 网上小找一下,发现解决办法都很奇怪,还有的谁是recycleview的bug,喝喝 2  这个bug 主要是你在刷新的时候 调用了list.clear 并且还滑动了 3 解决办法 在list.clear() 之后在 adapter_.notifyDataSetChanged(); 4 还有就是在那个返回值之后再去 list.clear(); 5 如果不对留言讨论

RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)

转自:RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案 RecyclerView是Android-support-v7-21版本中新增的一个Widget,RecyclerView在刷新数据的时候会出现以下异常: [java] view plain copy java.lang.IndexOutOfBoundsException: Inconsis

常见异常(1)— java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

异常: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 原因:对于一个list在获取其中的首个元素list.get(0)时,未做判断list是否存在元素的判断 改正:if(null!=list && list.size() !=0){  list.get(0);} 注:list=null 是判断是否存在这样一个list ,list.get(0) 是指list中是否有元素(前提是这个list已经存在) 原文地址:https://w

Spring的java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!异常处理方法

使用Spring提供的模板类HibernateDaoSupport,如果单纯的使用'命名参数'的形式编写HQL语句如: 1 public class UserDaoImpl extends HibernateDaoSupport implements IUser { 2 3 4 @Override 5 public User login(String userName, String pwd) { 6 String hql="from User as u where u.username=:us

【Spring】java.lang.IndexOutOfBoundsException: Index: 256, Size: 256

Spring接受前台的数据超过256出现如下异常: org.springframework.beans.InvalidPropertyException: Invalid property 'specificationValues[256]' of bean class [com.sencloud.entity.Specification]: Index of out of bounds in property path 'specificationValues[256]'; nested ex

报错: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

描述:使用Hibernate登陆验证时故意输入没有的用户名,所产生的错误. 错误代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /*      * 根据客户的用户名查找客户信息      */     @Override     public User getUserByUserName(String userName) {         String hql="from User where userName=?";         Session s

hive脚本出现Error: java.lang.RuntimeException: Error in configuring object和Caused by: java.lang.IndexOutOfBoundsException: Index: 9, Size: 9

是在reduce阶段报的错误,详细错误信息是 朱传豪 19:04:48 Diagnostic Messages for this Task: Error: java.lang.RuntimeException: Error in configuring object at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:109) at org.apache.hadoop.util.ReflectionU