bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
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
Public methods |
void
|
attach()
Link the TabLayout and the ViewPager2 together.
|
void
|
detach()
Unlink the TabLayout and the ViewPager.
|
boolean
|
isAttached()
Returns whether the TabLayout and the ViewPager2 are linked together.
|
Inherited methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
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.