ViewPager2 default position

hosseinAmini picture hosseinAmini · May 26, 2019 · Viewed 8.4k times · Source

I'm creating a slideshow with ViewPager2. For example, the slideshow has 3 items and I want to show the second item when the activity opens. I use setCurrentItem(int item, boolean smoothScroll) method but it doesn't work and nothing happens. How can I achieve it?

viewPager.adapter = adapter
viewPager.setCurrentItem(1, true)

Answer

Rune picture Rune · Mar 30, 2020

I think an easier more reliable fix is to defer to next run cycle instead of unsecure delay e.g

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