滚动RecyclerView获取异常: - " java.lang.IndexOutOfBoundsException:检测到不一致。商品位置无效21"

时间:2017-10-11 12:39:08

标签: java android android-recyclerview recycler-adapter

我在我的应用中使用RecyclerViewSqlite。 我已经从RecyclerView加载SQlite而不是后台中的呼叫后服务,并尝试通过RecyclerView方法通知notifyDataSetChanged(),但在滚动{时遇到意外异常{1}}(当RecyclerView正在更新或通知时)。​​

请检查我用于通知的RecyclerView并在setAdapter()上设置适配器

RecyclerView

我已经尝试了所有 private void setAdapter() { if (adapter == null) { adapter = new EmojiAdapter(stickerArrayList, getActivity(), "Sticker", stickerIdArrayList); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); rvEmoji.setLayoutManager(mLayoutManager); rvEmoji.setItemAnimator(new DefaultItemAnimator()); rvEmoji.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } } RecyclerView.stopScroll(),但问题并未缩短

异常

  

java.lang.IndexOutOfBoundsException:检测到不一致。无效的项目位置21(偏移量:21).state:32                                                                 在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5546)                                                                 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5482)                                                                 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5478)                                                                 在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2215)                                                                 在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1542)                                                                 在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502)                                                                 在android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1316)                                                                 在android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1061)                                                                 在android.support.v7.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:4769)                                                                 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)                                                                 在android.view.Choreographer.doCallbacks(Choreographer.java:574)                                                                 在android.view.Choreographer.doFrame(Choreographer.java:543)                                                                 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)                                                                 在android.os.Handler.handleCallback(Handler.java:733)                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                 在android.os.Looper.loop(Looper.java:136)                                                                 在android.app.ActivityThread.main(ActivityThread.java:5095)                                                                 at java.lang.reflect.Method.invokeNative(Native Method)                                                                 在java.lang.reflect.Method.invoke(Method.java:515)                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)                                                                 在dalvik.system.NativeStart.main(本地方法)

有很多类似问题,但我没有得到预期的结果。这里是我在此处发布问题之前访问过的链接。
1. First link
2. Second link
3. Third link
4. Forth link
5. Fifth link
6. Sixth link

我已经访问了所有链接并尝试了他们最好的解决方案,但它对我不起作用..

我正在使用以下gradle版本的RecyclerView

RecyclerView.getRecycledViewPool().clear()

请检查我正在使用的Adapter类,如下所示: -

compile 'com.android.support:recyclerview-v7:26.+'

请帮我解决问题。谢谢

  

更新
  我已经做了什么来解决我已经完成的冲突,在加载数据时禁用用户界面(显示一些进度条)。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题“java.lang.IndexOutOfBoundsException:检测到不一致。无效的项目位置6(偏移量:6).state”。使用pullrefrash显示此崩溃,当你有加载时,列表比你第一次加载更多。然后你pulltorfrash新数据并清除旧的列表数据,此时你的适配器的数据大小不超过你的新数据大小。

答案 1 :(得分:1)

尝试评论这行代码:

rvEmoji.setItemAnimator(new DefaultItemAnimator());

检查它现在是否有效。如果确实如此,那么您遇到的错误是因为启用了PredictiveAnimations。要将代码与动画一起使用,您必须子类化您正在使用的特定布局管理器,并覆盖 supportsPredictiveItemAnimations()方法并返回false。

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

详细了解一下here