这是第一次使用ViewPager2,我正在尝试创建动态页面,以便用户可以随时删除任何页面。要删除我执行的页面。
tabs.removeAt(index)
stateAdapter.notifyItemRemoved(index)
当我删除最后一个索引时,它工作正常,但是当我尝试在最后一个索引之前删除任何索引时,将引发以下异常。
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:-1).state:3 androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{96905c8 VFED..... ......ID 0,0-720,1275 #1}, adapter:app.spidy.browser.adapters.PagerStateAdapter@22a9661, layout:androidx.viewpager2.widget.ViewPager2$LinearLayoutManagerImpl@5f9c586, context:app.spidy.spider.activities.WebBotEditorActivity@cf13bdd
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6183)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:675)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4085)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at androidx.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1213)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:899)
at com.google.android.material.bottomsheet.BottomSheetBehavior.onLayoutChild(BottomSheetBehavior.java:380)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
2020-06-25 08:20:18.627 E/AndroidRuntime: at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
at android.view.View.layout(View.java:22080)
at android.view.ViewGroup.layout(ViewGroup.java:6330)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3209)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2705)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1791)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7867)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:967)
at android.view.Choreographer.doCallbacks(Choreographer.java:791)
at android.view.Choreographer.doFrame(Choreographer.java:726)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7386)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:514)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
以下是我的FragmentStateAdapter
适配器
class PagerStateAdapter(
private val tabs: List<Tab>,
fragmentManager: FragmentManager,
lifecycle: Lifecycle
) : FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int = tabs.size
override fun createFragment(position: Int): Fragment = tabs[position].fragment
}