问题
Removing/Adding fragments at index results in unexpected behaviour in Viewpager2. This was not possible with ViewPager
but expected to work with Viewpager2
. It causes duplicate fragments and out of sync TabLayout
.
Here is a demo project which reproduces this issue. There is a toggle button which removes a fragment and reattaches it at a particular index. In this case attached fragment should be green but it's blue and there are 2 blue fragments somehow.
here is how my adapter looks
class ViewPager2Adapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
val fragmentList: MutableList<FragmentName> = mutableListOf()
override fun getItemCount(): Int {
return fragmentList.size
}
override fun createFragment(position: Int): Fragment {
return when (fragmentList[position]) {
FragmentName.WHITE -> WhiteFragment()
FragmentName.RED -> RedFragment()
FragmentName.GREEN -> GreenFragment()
FragmentName.BLUE -> BlueFragment()
}
}
fun add(fragment: FragmentName) {
fragmentList.add(fragment)
notifyDataSetChanged()
}
fun add(index: Int, fragment: FragmentName) {
fragmentList.add(index, fragment)
notifyDataSetChanged()
}
fun remove(index: Int) {
fragmentList.removeAt(index)
notifyDataSetChanged()
}
fun remove(name: FragmentName) {
fragmentList.remove(name)
notifyDataSetChanged()
}
enum class FragmentName {
WHITE,
RED,
GREEN,
BLUE
}
}
I have filed a bug with google as well
回答1:
Turns out that you need to override these two methods if you are working with mutable
collections in ViewPager2
override fun getItemId(position: Int): Long {
return fragmentList[position].ordinal.toLong()
}
override fun containsItem(itemId: Long): Boolean {
val fragment = FragmentName.values()[itemId.toInt()]
return fragmentList.contains(fragment)
}
Adding these two in my current adapter fixes the problem
来源:https://stackoverflow.com/questions/61406176/viewpager2-not-able-to-dynamically-add-remove-fragment