Remove page change listener before changing adapter

This commit is contained in:
Naveen 2022-06-19 17:19:35 +05:30
parent ccb9b60a12
commit e6039d07cb

View file

@ -462,11 +462,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
if (!isDestroyed) {
pagerAdapter.shouldInitFragment = mPos < 5
view_pager.apply {
// must remove the listener before changing adapter, otherwise it might cause `mPos` to be set to 0
removeOnPageChangeListener(this@ViewPagerActivity)
adapter = pagerAdapter
pagerAdapter.shouldInitFragment = true
currentItem = mPos
removeOnPageChangeListener(this@ViewPagerActivity)
addOnPageChangeListener(this@ViewPagerActivity)
currentItem = mPos
}
}
}