我的RecyclerView有问题,我了解到here该错误已修复,并且我应该确保在主线程上插入数据并调用适当的notify。我做到了,但是我仍然遇到这个例外。

onLoadFinisher方法(我正在使用加载器加载更多数据)

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
{
    switch (loader.getId())
    {
        case UP_LOADER_ID:
            Log.d("Adapter", "Thread: "+Thread.currentThread().getName());
            days.put(firstDate, data);
            notifyItemRangeInserted(0, data.getCount()+1);
            loadingUp = false;
            break;

        case DOWN_LOADER_ID:
            days.put(lastDate, data);
            notifyItemRangeInserted(getItemCount(), data.getCount()+1);
            loadingDown = false;
            break;

        default:
            throw new IllegalArgumentException("Unknown loader id: " + loader.getId());
    }
}


日志

I/OpenGLRenderer: Initialized EGL, version 1.4
D/Adapter: Thread: main
D/Adapter: Thread: main
D/Adapter: Thread: main
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.antilopa, PID: 6406
                  java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{fa4fb40 position=42 id=-1497045600000, oldPos=21, pLpos:21 scrap [attachedScrap] tmpDetached not recyclable(1) no parent}
                      at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5220)
                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5402)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3457)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3252)
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
                      at android.view.View.layout(View.java:16722)
                      at android.view.ViewGroup.layout(ViewGroup.java:5438)

最佳答案

您可以将getItemCount()替换为getItemCount()-1,因为getItemCount()返回计数数量,但索引值为getItemCount()-1。

 case DOWN_LOADER_ID:
        days.put(lastDate, data);
        notifyItemRangeInserted(data.getCount()-1, data.getCount()+1);
        loadingDown = false;
        break;

关于android - Android RecyclerView:IndexOutOfBoundsException:检测到不一致,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44448235/

10-09 04:01