Skip to content

ViewPager

ViewPager

When using ViewPager, use DevViewPagerAdapter for fragment configuration:

val adapter = DevViewPagerAdapter(
    fm = supportFragmentManager, // Fragment Manager where you inflate ViewPager
    listFragment = arrayListOf(AllUserFragment(), FavoriteUserFragment()), // list of DevFragment
    listTitle = arrayListOf("All", "Favorite") // List of title for the fragment, nullable
)

ViewPager2

When using ViewPager2, just call method setup to configure it:

binding.vpMain.setup {
    // REQUIRED
    withActivity(this) // (1)

    withFragment(this) // (2)

    withListFragment(listOf(fragmentA, fragmentB)) // (3)

    // Optional
    bindWithTabLayout(binding.tabLayoutMain) { // (4)

        withListTitles(listOf("title A", "title B")) // (5)

        withListIcon(listOf(getDrawableResource(R.drawable.ic_list), getDrawableResource(R.drawable.ic_star))) // (6)
    }
}
  1. When ViewPager inflated inside Activity
  2. When ViewPager inflated inside Fragment
  3. Define list Fragment
  4. Bind ViewPager2 with TabLayout
  5. Add Title to TabLayout
  6. Add icon to the tab