gpt4 book ai didi

android - FragmentStateAdapter 抛出 java.lang.IllegalStateException : Design assumption violated after configuration change

转载 作者:行者123 更新时间:2023-12-02 12:10:49 25 4
gpt4 key购买 nike

FragmentStateAdapter 抛出

java.lang.IllegalStateException: Design assumption violated,



当设备旋转时。我该如何解决这个问题?
private inner class PagerAdapter : FragmentStateAdapter(this) {
private val ids = LongSparseArray<Media>()

override fun getItemCount(): Int = adapter.itemCount

override fun getItemId(position: Int): Long {
return adapter.source!![position].time
}

override fun containsItem(itemId: Long): Boolean {
return ids[itemId] != null
}
}

异常(exception)

 java.lang.IllegalStateException: Design assumption violated.
at androidx.viewpager2.adapter.FragmentStateAdapter.placeFragmentInViewHolder(FragmentStateAdapter.java:270)
at androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(FragmentStateAdapter.java:259)
at androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(FragmentStateAdapter.java:68)

最佳答案

我找到了解决方案。显然,您必须在其他配置更改后恢复 FragmentStateAdapter 的状态才能正确恢复 fragment 。

保存状态

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
model.addState(IDS, pagerAdapter.ids)
model.addState(LIST, (adapter.source as ListAdapterSource).getList())
}

恢复状态
private inner class PagerAdapter : FragmentStateAdapter(this) {
val ids: LongSparseArray<Media> = model.getState(IDS) ?: LongSparseArray()
...
}

关于android - FragmentStateAdapter 抛出 java.lang.IllegalStateException : Design assumption violated after configuration change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57352437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com