Home > Mobile >  Preselect tab in TabLayout with ViewPager2
Preselect tab in TabLayout with ViewPager2

Time:11-18

I have a ViewPager2 and a TabLayout that contains two tabs. How do I select second tab when first entering the page?

EDIT: I managed to select second tab like so: tabLayout.getTabAt(1).select() But I have another problem now, because the page under it is the page that corresponds to the first tab and viewPager.setCurrentItem(1, true) doesn't seem to fix it.

CodePudding user response:

You can use,

viewPager.post {
  viewPager.setCurrentItem(1, false)
}

Pass false to directly jump to second page instead of a smooth scroll.

  • Related