RecyclerView java.lang.IndexOutOfBoundsException:检测到不一致。视图支架适配器位置无效。由于更改 getItemCount()

Posted

技术标签:

【中文标题】RecyclerView java.lang.IndexOutOfBoundsException:检测到不一致。视图支架适配器位置无效。由于更改 getItemCount()【英文标题】:RecyclerView java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter position. Due to changing getItemCount() 【发布时间】:2017-11-02 23:51:20 【问题描述】:

我得到一个java.lang.IndexOutOfBoundsException: Inconsistency detectedRecyclerView,但只有当我似乎要扔掉它时。当我滚动得足够慢时,它不会在我身上崩溃。

我将我的基本适配器用于许多其他子类,但只有某些子类会崩溃。知道为什么会这样吗?

编辑

我发现发生这种情况的原因是因为getItemCount() 会根据页脚加载视图的状态而变化。我相信这会在更改和滚动时导致不一致。

我通过始终在 getItemCount() 中为我的页眉/页脚加载视图返回 2 个额外项目来测试这一点。而不是根据它们的状态动态计算计数。解决此问题的好方法是什么?

我的适配器

public abstract class BaseRecyclerAdapter<T>
        extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements LoadingAndErrorViewHolder.LoadingViewHolderListener 

    public static final int STATUS_HIDDEN = 0;
    public static final int STATUS_LOADING = 1;
    public static final int STATUS_ERROR = 2;

    protected static final int TYPE_HEADER_LOADING = 0;
    protected static final int TYPE_FOOTER_LOADING = 1;
    protected static final int TYPE_NORMAL = 2;

    private static final int POSITION_TYPE_HEADER_LOADING = 0;

    private BaseRecyclerAdapterListener mBaseRecyclerAdapterListener;
    private final List<T> mBackingList = new ArrayList<T>();
    private int mHeaderStatus = STATUS_HIDDEN;
    private int mFooterStatus = STATUS_HIDDEN;
    private final int mNumOfHeaderItems;

    public interface BaseRecyclerAdapterListener 
        void onErrorHeaderRetryClicked();
        void onErrorFooterRetryClicked();
    

    public BaseRecyclerAdapter() 
        this(null);
    

    public BaseRecyclerAdapter(BaseRecyclerAdapterListener listener) 
        this(0, listener);
    

    public BaseRecyclerAdapter(int numOfHeaderItems, BaseRecyclerAdapterListener listener) 
        mNumOfHeaderItems = numOfHeaderItems;
        mBaseRecyclerAdapterListener = listener;
        setHasStableIds(true);
    

    @Override
    public long getItemId(int position) 
        final T item = getItem(position);
        if (item != null) 
            return item.hashCode();
         else 
            return position;
        
    

    @Override
    public int getItemCount() 
        int count = mBackingList.size();
        if (mHeaderStatus != STATUS_HIDDEN) 
            count++;
        
        if (mFooterStatus != STATUS_HIDDEN) 
            count++;
        
        return mNumOfHeaderItems + count;
    

    @Override
    public int getItemViewType(int position) 
        final int headerAdjustment = mHeaderStatus != STATUS_HIDDEN ? 1 : 0;
        if (position == 0 && mHeaderStatus != STATUS_HIDDEN) 
            return TYPE_HEADER_LOADING;
         else if (position < mBackingList.size() + headerAdjustment + mNumOfHeaderItems) 
            return TYPE_NORMAL;
         else 
            return TYPE_FOOTER_LOADING;
        
    

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) 
            case TYPE_HEADER_LOADING: 
                return new LoadingAndErrorViewHolder(inflater.inflate(
                        LoadingAndErrorViewHolder.LAYOUT_ID, parent, false),
                        LoadingAndErrorViewHolder.TYPE_HEADER, this);
            
            case TYPE_FOOTER_LOADING: 
                return new LoadingAndErrorViewHolder(inflater.inflate(
                        LoadingAndErrorViewHolder.LAYOUT_ID, parent, false),
                        LoadingAndErrorViewHolder.TYPE_FOOTER, this);
            
            default: 
                return onCreateItemViewHolder(parent, viewType);
            
        
    

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 
        final int viewType = getItemViewType(position);
        switch (viewType) 
            case TYPE_HEADER_LOADING: 
                ((LoadingAndErrorViewHolder) holder).setStatus(mHeaderStatus);
                break;
            
            case TYPE_FOOTER_LOADING: 
                ((LoadingAndErrorViewHolder) holder).setStatus(mFooterStatus);
                break;
            
            default: 
                onBindItemViewHolder(holder, position);
            
        
    

    @Override
    public void onHeaderLoadingViewHolderClick() 
        if (mBaseRecyclerAdapterListener != null) 
            mBaseRecyclerAdapterListener.onErrorHeaderRetryClicked();
        
    

    @Override
    public void onFooterLoadingViewHolderClick() 
        if (mBaseRecyclerAdapterListener != null) 
            mBaseRecyclerAdapterListener.onErrorFooterRetryClicked();
        
    

    public T getItem(int position) 
        final int headerAdjustment = mHeaderStatus != STATUS_HIDDEN ? 1 : 0;
        final int adjustedPosition = position - headerAdjustment - mNumOfHeaderItems;
        if (adjustedPosition < 0
                || (mFooterStatus != STATUS_HIDDEN && position == getItemCount() - 1)) 
            return null;
         else 
            return mBackingList.get(adjustedPosition);
        
    

    public List<T> getList() 
        return mBackingList;
    

    public int getListSize() 
        return mBackingList.size();
    

    public void setList(List<T> list) 
        if (list != null) 
            clear();
            add(list);
        
    

    public void add(T item) 
        if (item != null) 
            mBackingList.add(item);
            notifyItemInserted(getItemCount() - 1);
        
    

    public void add(int index, T item) 
        if (item != null) 
            mBackingList.add(index, item);
            notifyItemInserted(index + getItemOffset());
        
    

    public void add(List<T> list) 
        if (list != null) 
            final int currentLastIndex;
            if (mBackingList.size() > 0) 
                currentLastIndex = mBackingList.size() - 1;
             else 
                currentLastIndex = 0;
            
            mBackingList.addAll(list);
            notifyItemRangeInserted(currentLastIndex + getItemOffset(), list.size());
        
    

    public void add(int index, List<T> list) 
        if (list != null) 
            mBackingList.addAll(index, list);
            notifyItemRangeInserted(index + getItemOffset(), list.size());
        
    

    public void update(int position, T item) 
        mBackingList.set(position, item);
        notifyItemChanged(position + getItemOffset());
    

    public void remove(T item) 
        if (item != null && mBackingList.remove(item)) 
            notifyDataSetChanged();
        
    

    public void clear() 
        mBackingList.clear();
        notifyDataSetChanged();
    

    public void setHeaderStatus(int headerStatus) 
        mHeaderStatus = headerStatus;
        notifyItemChanged(POSITION_TYPE_HEADER_LOADING);
    

    public void setFooterStatus(int footerStatus) 
        mFooterStatus = footerStatus;
        notifyItemChanged(getItemCount() - 1);
    

    public void checkEndOfList(List<T> data) 
        if (data.isEmpty()) 
            setFooterStatus(STATUS_HIDDEN);
         else 
            setFooterStatus(STATUS_LOADING);
        
    

    protected int getHeaderStatus() 
        return mHeaderStatus;
    

    protected int getFooterStatus() 
        return mFooterStatus;
    

    protected abstract RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType);

    protected abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);

    private int getItemOffset() 
        final int headerAdjustment = mHeaderStatus != STATUS_HIDDEN ? 1 : 0;
        return headerAdjustment + mNumOfHeaderItems;
    

堆栈跟踪

06-01 10:46:36.230 11448-11448/com.fusionprojects.edmodo E/androidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.fusionprojects.edmodo, PID: 11448
                                                                           java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolderdcf5c3a position=23 id=-1, oldPos=23, pLpos:-1 scrap [attachedScrap] tmpDetached no parent
                                                                               at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                               at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
                                                                               at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
                                                                               at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
06-01 10:46:36.231 11448-11448/com.fusionprojects.edmodo E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                               at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
                                                                               at android.view.View.layout(View.java:17641)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                               at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

RecyclerView: Inconsistency detected. Invalid item position的可能重复 【参考方案1】:

我通过在我的setFooterStatus() 方法中调用notifyDataSetChanged() 解决了这个问题。

【讨论】:

以上是关于RecyclerView java.lang.IndexOutOfBoundsException:检测到不一致。视图支架适配器位置无效。由于更改 getItemCount()的主要内容,如果未能解决你的问题,请参考以下文章

recyclerview 内的 RecyclerView 只显示父 recyclerview 的最后一项

RecyclerView详解

RecyclerView

RecyclerView嵌套RecyclerView问题

RecyclerView小结

RecyclerView系列:RecyclerView嵌套RecyclerView(BaseRecyclerViewAdapterHelper实现)