fragment change in Viewpager2 on Button Click

Issue

Can we change fragment in viewpager2 with button click not on tablayout ?
Or there is way make tab layout as button with some corner radius and gap between them?

Solution

Yes you can do this by this function -> setCurrentItem which takes two parameters the first is the index of fragment that you want, the second is a boolean if you want to change fragment with smooth scroll set it to true:

binding.pager.setCurrentItem(1, false)

Answered By – Mohamed Rejeb

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published