Skip to content

Commit

Permalink
[TabLayout] Fix bug with selected tab text appearance when swiping in…
Browse files Browse the repository at this point in the history
… viewpager.

PiperOrigin-RevId: 489559352
  • Loading branch information
imhappi authored and dsn5ft committed Nov 18, 2022
1 parent cb1905d commit 4a0e1a0
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/java/com/google/android/material/tabs/TabLayout.java
Expand Up @@ -1890,7 +1890,7 @@ void setScrollAnimatorListener(ValueAnimator.AnimatorListener listener) {
}

/**
* Called when a selected tab is added. Unselects all other tabs in the TabLayout.
* Called when a tab is selected. Unselects all other tabs in the TabLayout.
*
* @param position Position of the selected tab.
*/
Expand All @@ -1899,6 +1899,16 @@ private void setSelectedTabView(int position) {
if (position < tabCount) {
for (int i = 0; i < tabCount; i++) {
final View child = slidingTabIndicator.getChildAt(i);
// Update the tab view if it needs to be updated (eg. it's newly selected and it is not
// yet selected, or it is selected and something else was selected).
if ((i == position && !child.isSelected()) || (i != position && child.isSelected())) {
child.setSelected(i == position);
child.setActivated(i == position);
if (child instanceof TabView) {
((TabView) child).updateTab();
}
continue;
}
child.setSelected(i == position);
child.setActivated(i == position);
}
Expand Down Expand Up @@ -1951,15 +1961,9 @@ public void selectTab(@Nullable final Tab tab, boolean updateIndicator) {
// If the current tab is still attached to the TabLayout.
if (currentTab != null && currentTab.parent != null) {
dispatchTabUnselected(currentTab);
if (currentTab.view != null) {
currentTab.view.update();
}
}
if (tab != null) {
dispatchTabSelected(tab);
if (tab.view != null) {
tab.view.update();
}
}
}
}
Expand Down Expand Up @@ -2695,7 +2699,7 @@ void reset() {
setSelected(false);
}

final void update() {
final void updateTab() {
final Tab tab = this.tab;
final View custom = tab != null ? tab.getCustomView() : null;
if (custom != null) {
Expand Down Expand Up @@ -2771,8 +2775,12 @@ final void update() {
// has been explicitly set.
setContentDescription(tab.contentDesc);
}
}

final void update() {
updateTab();
// Finally update our selected state
setSelected(tab != null && tab.isSelected());
setSelected(this.tab != null && this.tab.isSelected());
}

private void inflateAndAddDefaultIconView() {
Expand Down

0 comments on commit 4a0e1a0

Please sign in to comment.