I want to use com.google.android.material.tabs.TabLayout
component with Android\'s new ViewPager
implementation androidx.viewpager2.widget.Vi
You can use Kotlin extension function:
fun TabLayout.setupWithViewPager(viewPager: ViewPager2, labels: List) {
if (labels.size != viewPager.adapter?.itemCount)
throw Exception("The size of list and the tab count should be equal!")
TabLayoutMediator(this, viewPager,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
tab.text = labels[position]
}).attach()
}
And call it:
tabLayout.setupWithViewPager(viewPager, listOf("Tab A", "Tab B"))