关于recyclerView快速上下滑动时的崩溃问题

转载自http://blog.csdn.net/w_lin/article/details/53019888

最近发现recyclerView快速滑动时会崩溃,百度发现是Inconsistency detected. Invalid item position 16(offset:16).state:25异常,因此我对事件进行拦截,发现依旧不查了半天才找到这个博主写的解决了我的问题,分享给大家,首先我们需要重写LinearLayoutManager方法

昨天遇到一个神奇并且很难缠的bug……
RecyclerView上下滑动的时候崩溃,崩溃信息如下:

java.lang.IndexOutOfBoundsException
Inconsistency detected. Invalid item position 16(offset:16).state:64
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4957)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)
android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4357)
android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
android.view.Choreographer.doCallbacks(Choreographer.java:606)
android.view.Choreographer.doFrame(Choreographer.java:574)
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:135)
android.app.ActivityThread.main(ActivityThread.java:5601)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

搜遍了Google,百度,github,stackoverflow……都没有解决,算了自己动手丰衣足食。经过两天研究这么解决了:

/**
 * 去除动画防止,防止出现recyclerView的bug
 * Created by zhanglin on 2016/11/3.
 */

public class MyLinearLayoutManager extends LinearLayoutManager {
    public MyLinearLayoutManager(Context context) {
        super(context);
    }

    @Override
    public boolean supportsPredictiveItemAnimations() {
        return false;
    }

    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();

        }
    }

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            return super.scrollVerticallyBy(dy, recycler, state);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

Java中如下:

MyLinearLayoutManager myLinearLayoutManager = new MyLinearLayoutManager (mContext);
recyclerView.setLayoutManager(myLinearLayoutManager );
   
   
  • 1
  • 2

解决了,好开心哈哈哈

            <link rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-d4dade9c33.css">
                </div>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值