解决 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 8(offset:8).st

08-07 09:51:13.414 26357-26357/..............android E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.wkhgs.b2b2c2016.android, PID: 26357
                                                                             java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 8(offset:8).state:20
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4368)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
                                                                                 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
                                                                                 at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
                                                                                 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
                                                                                 at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1155)
                                                                                 at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1012)
                                                                                 at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3777)
                                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
                                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:742)
                                                                                 at android.view.Choreographer.doFrame(Choreographer.java:671)
                                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919)
                                                                                 at android.os.Handler.handleCallback(Handler.java:761)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                 at android.os.Looper.loop(Looper.java:156)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6577)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)

                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)    前段时间遇到的一个BUG算是老BUG

重写下LinearLayoutManager 就可以解决这个问题。

public class WrapContentLinearLayoutManager extends LinearLayoutManager {

    public WrapContentLinearLayoutManager(Context context) {
        super(context);
    }

    public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @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;
    }

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值