Skip to content

Instantly share code, notes, and snippets.

@JoaquimLey
Last active November 19, 2019 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoaquimLey/48895dc971b450b686c31f9af05d0ddd to your computer and use it in GitHub Desktop.
Save JoaquimLey/48895dc971b450b686c31f9af05d0ddd to your computer and use it in GitHub Desktop.
// Extension function
fun TabLayout.setupWithViewPager2(
viewPager: ViewPager2,
callback: (TabLayout.Tab, Int) -> Unit
) {
TabLayoutMediator(this, viewPager) { tab, position ->
callback.invoke(tab, position)
}.attach()
}
// Use, make sure to set an adapter to the view pager or you'll get an IllegalStateException.
// view_pager is a ViewPager2 instance.
tab_layout.setupWithViewPager2(view_pager) { tab, position ->
when (position) {
0 -> tab.text = "TAB ONE"
1 -> tab.text = "TAB TWO"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment