Last active
November 19, 2019 19:04
-
-
Save JoaquimLey/48895dc971b450b686c31f9af05d0ddd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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