Recycler视图数组索引outof bound Incossitency错误,同时SwipetoRefresh和Recycler同时滚动

时间:2016-02-12 05:51:16

标签: android android-recyclerview indexoutofboundsexception swiperefreshlayout

我正在使用23.1.1 RecyclerView,当刷卡刷新开始时,在这种情况下,当我滚动我的RecyclerView它会崩溃并给我错误

这是我的ErrorLog,

    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{39e0644b position=6 id=-1, oldPos=-1, pLpos:-1 no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4251)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4382)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1161)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1018)
at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1492)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2416)
at android.view.View.dispatchTouchEvent(View.java:8476)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2407)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
at android.app.Activity.dispatchTouchEvent(Activity.java:2752)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
at android.view.View.dispatchPointerEvent(View.java:8671)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4193)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4059)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3604)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:36

只有当我使用滑动刷新布局时才会出现这些错误。

在适配器的onBindViewHolder方法中,当最后一项可见时,我调用loadmore数据的事件

@Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


        if(position == data.size() -1 && data.size() >= AppConstants.LIST_PAGE_SIZE){

            EventBus.getDefault().postSticky(new LoadMoreDataEvent(getPageIndex()));
        }
}

更新

清除列表后,当我试图通知适配器我收到此错误时,这些是回收站视图中的错误。我只是清除列表并设置适配器null及其工作。

list.clear();
mAdapter.notifyDataSetChanged();

2 个答案:

答案 0 :(得分:1)

您可能会在回收站视图中遇到错误。它已经发布在这里 - RecyclerView: Inconsistency detected. Invalid item position

对于错误跟踪,您可以查看herehere

答案 1 :(得分:0)

由于列表已清除,因此在运行后台任务时获取正确位置会出现问题,因此当您在notifydatasetchange之前清除回收视图时,请不要使用它。使用此:

<div class="row">
  <div class="col" style="height: 200px;"><div>My Y center is always 100px;</div></div>
  <div class="col" style="height: 100vh;"><div>My Y xenter varies with Devtools/etc.</div></div>
</div>

而不是

mAdapter.notifyItemRangeInserted(0, YourArrayList.size())