У меня есть следующий фрагмент кода:
for (child in children)
{
items.add(book)
viewAdapter.notifyItemInserted(items.count()-1)
}
Это ошибка, которую я получаю:
E / AndroidRuntime: FATAL EXCEPTION: main
Процесс: edu.uoc.android.pac2, PID: 6949
java.lang.IndexOutOfBoundsException: обнаружено несоответствие. Недопустимое положение адаптера держателя вида ViewHolder {c7d5aa8 position = 10
id = -1, oldPos = 1, pLpos: 1 записка [attachScrap] tmpDetached нет родителя}
android.support.v7.widget.RecyclerView {a74a62 VFED ..... ...... I.
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.jpg) .jpgViewRootImpl $ TraversalRunnable.run (ViewRootImpl.java:7183) в android.view.Choreographer $ CallbackRecord.run (Choreographer.java:949) в android.view.Choreographer.doCallbacks (Choreographer.hoava: atre:.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) * только 1007 *
происходит, когда я добавляю элементы, работает следующий код:
items.clear()
viewAdapter.notifyItemRangeRemoved(0, size)
Также это:
val position = items.indexOf(elem)
items.remove(elem)
viewAdapter.notifyItemRemoved(position)
Чего мне не хватает?