通知适配器上的RecyclerView不一致

时间:2019-05-08 17:28:16

标签: android android-recyclerview notifydatasetchanged

我有以下代码:

for (child in children)
{
   items.add(book)
   viewAdapter.notifyItemInserted(items.count()-1)
}

这是我得到的错误:

  

E / Android运行时:致命异常:主要       流程:edu.uoc.android.pac2,PID:6949       java.lang.IndexOutOfBoundsException:检测到不一致。无效的视图支架适配器positionViewHolder {c7d5aa8 position = 10   id = -1,oldPos = 1,pLpos:1个废料[attachedScrap] tmpDetached没有父级}   android.support.v7.widget.RecyclerView {a74a62 VFED .....   0,0-1080,1536#7f08002b app:id / book_recycler_view},   适配器:edu.uoc.android.pac2.SimpleItemRecyclerViewAdapter@9b0b7f3,   布局:android.support.v7.widget.StaggeredGridLayoutManager@13c8cb0,   上下文:edu.uoc.android.pac2.BookListActivity@23f070d           在android.support.v7.widget.RecyclerView $ Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)           在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5854)           在android.support.v7.widget.LayoutState.next(LayoutState.java:100)           在android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1609)           在android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:683)           在android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:605)           在android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)           在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)           在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1565)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在com.android.internal.policy.DecorView.onLayout(DecorView.java:753)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)           在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)           在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)           在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7183)           在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:949)           在android.view.Choreographer.doCallbacks(Choreographer.java:761)           在android.view.Choreographer.doFrame(Choreographer.java:696)           在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:935)           在android.os.Handler.handleCallback(Handler.java:873)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

仅当我添加项目时,以下代码才有效:

items.clear()
viewAdapter.notifyItemRangeRemoved(0, size)

也是这样:

val position = items.indexOf(elem)
items.remove(elem)
viewAdapter.notifyItemRemoved(position)

我缺少什么?

1 个答案:

答案 0 :(得分:1)

for (child in children)
{
  items.add(book)
}
viewAdapter.notifyItemInserted(items.count()-1)

请保持notifyItemInserted在循环之外并进行检查。