RecyclerView 刷新问题

RecyclerView 数据刷新不一致造成的bug. 我们使用了 方法一

Adapter内部的数据集合叫内部数据,通过添加到Adapter的数据叫外部数据。

正常情况我们使用adapter.addItems(),再通过notifyDataSetChanged();等方法更新UI视图,结果正常。但有时我们改变了外部数据,并没有及时的调用 adapter 更新视图,当再修改数据后再刷新,会出现这问题。

java.lang.IndexOutOfBoundsException

Inconsistency detected. Invalid view holder adapter positionViewHolder{eefae87 position=1 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} android.support.v7.widget.RecyclerView

具体日志记录

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2064e5c6 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4505)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4636)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2979)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2904)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1948)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1724)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.Line  

方法一

自己写一个继承LinearLayoutManager的包装类,在onLayoutChildren()方法里try-catch捕获该异常。

public class LinearLayoutManagerWrapper extends LinearLayoutManager {

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

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

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

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

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (Exception e) {
        }
    }
}

方法二

在进行数据移除和数据增加时,务必要保证RecyclerView的Adapter中的数据集和移除/添加等操作后的数据集保持一致!
外部数据集同步到内部数据集,使用如下的方法:

notifyItemRangeRemoved();
notifyItemRangeInserted();
notifyItemRangeChanged();
notifyDataSetChanged();

方法三

使用notifyDataSetChanged同步外部数据集和内部数据集。该方法简单,但是失去了动画效果,并且更新数据的性能低。

另外,如果对外部数据集做了二次以上的操作,却只调用notifyDataSetChanged同步一次,也很有可能会报上文所示的错误。

你可能感兴趣的:(RecyclerView 刷新问题)