TabLayoutMediator
public
final
class
TabLayoutMediator
extends Object
java.lang.Object | |
↳ | com.google.android.material.tabs.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 TabLayoutMediator.TabConfigurationStrategy
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 classes | |
---|---|
interface |
TabLayoutMediator.TabConfigurationStrategy
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
|
attach()
Link the TabLayout and the ViewPager2 together. |
void
|
detach()
Unlink the TabLayout and the ViewPager. |
boolean
|
isAttached()
Returns whether the |
Inherited methods | |
---|---|
Public constructors
TabLayoutMediator
public TabLayoutMediator (TabLayout tabLayout, ViewPager2 viewPager, TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy)
Parameters | |
---|---|
tabLayout |
TabLayout |
viewPager |
ViewPager2 |
tabConfigurationStrategy |
TabLayoutMediator.TabConfigurationStrategy |
TabLayoutMediator
public TabLayoutMediator (TabLayout tabLayout, ViewPager2 viewPager, boolean autoRefresh, TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy)
Parameters | |
---|---|
tabLayout |
TabLayout |
viewPager |
ViewPager2 |
autoRefresh |
boolean |
tabConfigurationStrategy |
TabLayoutMediator.TabConfigurationStrategy |
TabLayoutMediator
public TabLayoutMediator (TabLayout tabLayout, ViewPager2 viewPager, boolean autoRefresh, boolean smoothScroll, TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy)
Parameters | |
---|---|
tabLayout |
TabLayout |
viewPager |
ViewPager2 |
autoRefresh |
boolean |
smoothScroll |
boolean |
tabConfigurationStrategy |
TabLayoutMediator.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 | |
---|---|
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.
Returns | |
---|---|
boolean |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2021-02-08 UTC.