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

时间:2017-06-01 17:55:56

标签: android android-recyclerview indexoutofboundsexception recycler-adapter

我正在java.lang.IndexOutOfBoundsException: Inconsistency detected获得RecyclerView,但只有当我似乎扔掉它时。当我滚动得足够慢时,它不会让我崩溃。

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

修改

我发现发生这种情况的原因是因为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 positionViewHolder{dcf5c3a 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)

1 个答案:

答案 0 :(得分:0)

我通过在notifyDataSetChanged()方法中调用setFooterStatus()来解决此问题。