TabLayoutMediator

public final class 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.

Summary

Nested types

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

Public constructors

TabLayoutMediator(
    TabLayout tabLayout,
    ViewPager2 viewPager,
    TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy
)
TabLayoutMediator(
    TabLayout tabLayout,
    ViewPager2 viewPager,
    boolean autoRefresh,
    TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy
)
TabLayoutMediator(
    TabLayout tabLayout,
    ViewPager2 viewPager,
    boolean autoRefresh,
    boolean smoothScroll,
    TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy
)

Public methods

void

Link the TabLayout and the ViewPager2 together.

void

Unlink the TabLayout and the ViewPager.

boolean

Returns whether the TabLayout and the ViewPager2 are linked together.

Public constructors

TabLayoutMediator

public TabLayoutMediator(
    TabLayout tabLayout,
    ViewPager2 viewPager,
    TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy
)

TabLayoutMediator

public TabLayoutMediator(
    TabLayout tabLayout,
    ViewPager2 viewPager,
    boolean autoRefresh,
    TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy
)

TabLayoutMediator

public TabLayoutMediator(
    TabLayout tabLayout,
    ViewPager2 viewPager,
    boolean autoRefresh,
    boolean smoothScroll,
    TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy
)

Public methods

attach

public void attach()

Link the TabLayout and the ViewPager2 together. Must be called after ViewPager2 has an adapter set. To be called on a new instance of TabLayoutMediator or if the ViewPager2's adapter changes.

Throws
java.lang.IllegalStateException

If the mediator is already attached, or the ViewPager2 has no adapter.

detach

public void detach()

Unlink the TabLayout and the ViewPager. To be called on a stale TabLayoutMediator if a new one is instantiated, to prevent holding on to a view that should be garbage collected. Also to be called before attach when a ViewPager2's adapter is changed.

isAttached

public boolean isAttached()

Returns whether the TabLayout and the ViewPager2 are linked together.