TabLayoutMediator

A mediator to link a TabLayout with a ViewPager2. The mediator will synchronize the ViewPager2's position with the selected tab when a tab is selected, and the TabLayout's scroll position when the user drags the ViewPager2. TabLayoutMediator will listen to ViewPager2's OnPageChangeCallback to adjust tab when ViewPager2 moves. TabLayoutMediator listens to TabLayout's OnTabSelectedListener to adjust VP2 when tab moves. TabLayoutMediator listens to RecyclerView's AdapterDataObserver to recreate tab content when dataset changes.

Establish the link by creating an instance of this class, make sure the ViewPager2 has an adapter and then call attach on it. Instantiating a TabLayoutMediator will only create the mediator object, attach will link the TabLayout and the ViewPager2 together. When creating an instance of this class, you must supply an implementation of in which you set the text of the tab, and/or perform any styling of the tabs that you require. Changing ViewPager2's adapter will require a detach followed by attach call. Changing the ViewPager2 or TabLayout will require a new instantiation of TabLayoutMediator.

Constructors

Link copied to clipboard
constructor(@NonNull tabLayout: TabLayout, @NonNull viewPager: ViewPager2, @NonNull tabConfigurationStrategy: TabLayoutMediator.TabConfigurationStrategy)
constructor(@NonNull tabLayout: TabLayout, @NonNull viewPager: ViewPager2, autoRefresh: Boolean, @NonNull tabConfigurationStrategy: TabLayoutMediator.TabConfigurationStrategy)
constructor(@NonNull tabLayout: TabLayout, @NonNull viewPager: ViewPager2, autoRefresh: Boolean, smoothScroll: Boolean, @NonNull tabConfigurationStrategy: TabLayoutMediator.TabConfigurationStrategy)

Types

Link copied to clipboard
A callback interface that must be implemented to set the text and styling of newly created tabs.

Functions

Link copied to clipboard
open fun attach()
Link the TabLayout and the ViewPager2 together.
Link copied to clipboard
open fun detach()
Unlink the TabLayout and the ViewPager.
Link copied to clipboard
open fun isAttached(): Boolean
Returns whether the TabLayout and the ViewPager2 are linked together.