Skip to content

[TabLayoutMediator] Support responding to adapter changes #4548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/java/com/google/android/material/tabs/TabLayoutMediator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_IDLE;
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_SETTLING;

import android.view.ViewTreeObserver;
import androidx.recyclerview.widget.RecyclerView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -39,8 +40,7 @@
* the mediator object, {@link #attach()} will link the TabLayout and the ViewPager2 together. When
* creating an instance of this class, you must supply an implementation of {@link
* 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 {@link #detach()} followed by
* {@link #attach()} call. Changing the ViewPager2 or TabLayout will require a new instantiation of
* tabs that you require. Changing the ViewPager2 or TabLayout will require a new instantiation of
* TabLayoutMediator.
*/
public final class TabLayoutMediator {
Expand All @@ -54,6 +54,7 @@ public final class TabLayoutMediator {

@Nullable private TabLayoutOnPageChangeCallback onPageChangeCallback;
@Nullable private TabLayout.OnTabSelectedListener onTabSelectedListener;
@Nullable private ViewPagerOnLayoutListener onLayoutListener;
@Nullable private RecyclerView.AdapterDataObserver pagerAdapterObserver;

/**
Expand Down Expand Up @@ -127,6 +128,13 @@ public void attach() {
onTabSelectedListener = new ViewPagerOnTabSelectedListener(viewPager, smoothScroll);
tabLayout.addOnTabSelectedListener(onTabSelectedListener);

// Here we'll add a listener that responds to adapter changes. Unlike ViewPager,
// ViewPager2 doesn't have a built-in OnAdapterChangeListener, so we'll use a
// slightly different approach that relies on the fact that changing the adapter
// always causes a layout pass (see b/135299048).
onLayoutListener = new ViewPagerOnLayoutListener();
viewPager.getViewTreeObserver().addOnGlobalLayoutListener(onLayoutListener);

// Now we'll populate ourselves from the pager adapter, adding an observer if
// autoRefresh is enabled
if (autoRefresh) {
Expand All @@ -151,8 +159,10 @@ public void detach() {
adapter.unregisterAdapterDataObserver(pagerAdapterObserver);
pagerAdapterObserver = null;
}
viewPager.getViewTreeObserver().removeOnGlobalLayoutListener(onLayoutListener);
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
viewPager.unregisterOnPageChangeCallback(onPageChangeCallback);
onLayoutListener = null;
onTabSelectedListener = null;
onPageChangeCallback = null;
adapter = null;
Expand Down Expand Up @@ -317,4 +327,16 @@ public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
populateTabsFromPagerAdapter();
}
}

private class ViewPagerOnLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {

@Override
public void onGlobalLayout() {
RecyclerView.Adapter<?> currentAdapter = viewPager.getAdapter();
if (adapter != currentAdapter) {
detach();
attach();
}
}
}
}