通知适配器上的 RecyclerView 不一致
RecyclerView Inconsistency on notify adapter
我有如下一段代码:
for (child in children)
{
items.add(book)
viewAdapter.notifyItemInserted(items.count()-1)
}
这是我得到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.uoc.android.pac2, PID: 6949
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{c7d5aa8 position=10
id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
android.support.v7.widget.RecyclerView{a74a62 VFED..... ......I.
0,0-1080,1536 #7f08002b app:id/book_recycler_view},
adapter:edu.uoc.android.pac2.SimpleItemRecyclerViewAdapter@9b0b7f3,
layout:android.support.v7.widget.StaggeredGridLayoutManager@13c8cb0,
context:edu.uoc.android.pac2.BookListActivity@23f070d
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LayoutState.next(LayoutState.java:100)
at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1609)
at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:683)
at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:605)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at 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)
我错过了什么?
for (child in children)
{
items.add(book)
}
viewAdapter.notifyItemInserted(items.count()-1)
请将 notifyItemInserted 保持在循环外并检查它。
我有如下一段代码:
for (child in children)
{
items.add(book)
viewAdapter.notifyItemInserted(items.count()-1)
}
这是我得到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main Process: edu.uoc.android.pac2, PID: 6949 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{c7d5aa8 position=10 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent} android.support.v7.widget.RecyclerView{a74a62 VFED..... ......I. 0,0-1080,1536 #7f08002b app:id/book_recycler_view}, adapter:edu.uoc.android.pac2.SimpleItemRecyclerViewAdapter@9b0b7f3, layout:android.support.v7.widget.StaggeredGridLayoutManager@13c8cb0, context:edu.uoc.android.pac2.BookListActivity@23f070d at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854) at android.support.v7.widget.LayoutState.next(LayoutState.java:100) at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1609) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:683) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:605) at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) at android.widget.LinearLayout.onLayout(LinearLayout.java:1565) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:753) at android.view.View.layout(View.java:20672) at android.view.ViewGroup.layout(ViewGroup.java:6194) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) at android.view.Choreographer.doCallbacks(Choreographer.java:761) at android.view.Choreographer.doFrame(Choreographer.java:696) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at 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)
我错过了什么?
for (child in children)
{
items.add(book)
}
viewAdapter.notifyItemInserted(items.count()-1)
请将 notifyItemInserted 保持在循环外并检查它。