I had a similar problem, if not the same. The solution was to change fragments
/tabs
/pages
in a specific order.
- The
FragmentStateAdapter
should be notified first of the change to the fragment.
- The
TabLayout
should then call selectTab
under a post
- The
ViewPager
should then call setCurrentItem
under a post
, inside the TabLayouts post
to selectTab
.
This may be overkill but it worked for me.
Example
//or some other notify depending on your use case.
fragmentStateAdapter.notifyItemInserted(position);
tabLayout.post(()->{
tabLayout.selectTab(tabLayout.getTabAt(position));
viewPager2.post(()->{
viewPager2.setCurrentItem(position);
//I noticed on older devices like API 19
//the viewPager wouldn't complete transformations
//so we call this.
viewPager2.requestTransform();
});
});