android – RecyclerView在更新时崩溃

前端之家收集整理的这篇文章主要介绍了android – RecyclerView在更新时崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中有一个RecyclerView一切都正常我在滚动时最后一个项目是可见的我正在添加一些更多的项目到底部,它是正常工作.现在问题是我有一个处理程序,每5秒运行一次,检查是否有任何更新的Web服务,如果有任何更新,我只是将数据加载到RecyclerView的顶部它工作正常,当我有没有滚动RecyclerView,但是当我滚动它,并且处理程序被调用(因为它每5秒钟调用),然后更新应用程序崩溃顶部的RecyclerView.

顶部更新功能

public void addOnTop(ArrayList<ItemPOJO> topItemList) {
    for (int i = topItemList.size() - 1; i >= 0; i--) {
        itemList.add(0,topItemList.get(i));
    }
    notifyItemRangeInserted(0,topItemList.size());
    notifyDataSetChanged();
}

这是错误日志:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{419b8760 position=5 id=-1,oldPos=-1,pLpos:-1 no parent}
        at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4214)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4345)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
        at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1155)
        at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1012)
        at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1363)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2227)
        at android.view.View.dispatchTouchEvent(View.java:7190)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2274)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2009)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1931)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1385)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1879)
        at android.view.View.dispatchPointerEvent(View.java:7370)
        at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3182)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3127)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4164)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4143)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4235)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4214)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4254)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:523)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreog

P.S .: com.android.support:recyclerview-v7:22.2.1被使用

解决方法

这个崩溃可能是由于在更新列表(由适配器使用)时调用了setViewHolder和onBind方法,但是没有调用notifyDataSetChanged().通常在更新列表时滚动查看列表,或者在运行列表时,会出现这种情况.

解:维护ItemPOJO的单独列表(tempList),并在该列表中追加topData.然后清除适配器使用的列表,使用tempList设置适配器列表,并调用notifyDataSetChanged().

原文链接:https://www.f2er.com/android/310315.html

猜你在找的Android相关文章