Skip to content

Commit

Permalink
[TabLayout] Fixing bug with animation flicker when using tabs with Vi…
Browse files Browse the repository at this point in the history
…ewPager2

PiperOrigin-RevId: 491460655
(cherry picked from commit 84b7bc3)
  • Loading branch information
imhappi authored and dsn5ft committed Dec 9, 2022
1 parent b73cc61 commit ab3f203
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/java/com/google/android/material/tabs/TabLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ public interface BaseOnTabSelectedListener<T extends Tab> {
private TabLayoutOnPageChangeListener pageChangeListener;
private AdapterChangeListener adapterChangeListener;
private boolean setupViewPagerImplicitly;
private int viewPagerScrollState;

// Pool we use as a simple RecyclerBin
private final Pools.Pool<TabView> tabViewPool = new Pools.SimplePool<>(12);
Expand Down Expand Up @@ -1566,6 +1567,10 @@ public void setTabsFromPagerAdapter(@Nullable final PagerAdapter adapter) {
setPagerAdapter(adapter, false);
}

void updateViewPagerScrollState(int scrollState) {
this.viewPagerScrollState = scrollState;
}

@Override
public boolean shouldDelayChildPressedState() {
// Only delay the pressed state if the tabs can scroll
Expand Down Expand Up @@ -3240,7 +3245,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
/** Immediately update the indicator position to the currently selected position. */
private void jumpIndicatorToSelectedPosition() {
// Don't update the indicator position if the scroll state is not idle.
if (pageChangeListener != null && pageChangeListener.scrollState != SCROLL_STATE_IDLE) {
if (viewPagerScrollState != SCROLL_STATE_IDLE) {
return;
}
final View currentView = getChildAt(getSelectedTabPosition());
Expand Down Expand Up @@ -3467,6 +3472,10 @@ public TabLayoutOnPageChangeListener(TabLayout tabLayout) {
public void onPageScrollStateChanged(final int state) {
previousScrollState = scrollState;
scrollState = state;
TabLayout tabLayout = tabLayoutRef.get();
if (tabLayout != null) {
tabLayout.updateViewPagerScrollState(scrollState);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ private static class TabLayoutOnPageChangeCallback extends ViewPager2.OnPageChan
public void onPageScrollStateChanged(final int state) {
previousScrollState = scrollState;
scrollState = state;
TabLayout tabLayout = tabLayoutRef.get();
if (tabLayout != null) {
tabLayout.updateViewPagerScrollState(scrollState);
}
}

@Override
Expand Down

0 comments on commit ab3f203

Please sign in to comment.