Skip to content

Commit 7c291af

Browse files
committed
[Tabs] Fix OnLayoutChangeListener accumulation on tab updates
Resolves material-components#5069
1 parent 644909b commit 7c291af

1 file changed

Lines changed: 9 additions & 26 deletions

File tree

lib/java/com/google/android/material/tabs/TabLayout.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,13 @@ public final class TabView extends LinearLayout {
25282528

25292529
private int defaultMaxLines = 2;
25302530

2531+
private final OnLayoutChangeListener updateBadgeBoundsOnLayoutChangeListener =
2532+
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
2533+
if (v.getVisibility() == VISIBLE) {
2534+
tryUpdateBadgeDrawableBounds(v);
2535+
}
2536+
};
2537+
25312538
public TabView(@NonNull Context context) {
25322539
super(context);
25332540
updateBackgroundDrawable(context);
@@ -2812,8 +2819,6 @@ final void updateTab() {
28122819
updateTextAndIcon(this.textView, this.iconView, /* addDefaultMargins= */ true);
28132820

28142821
tryUpdateBadgeAnchor();
2815-
addOnLayoutChangeListener(iconView);
2816-
addOnLayoutChangeListener(textView);
28172822
} else {
28182823
// Else, we'll see if there is a TextView or ImageView present and update them
28192824
if (customTextView != null || customIconView != null) {
@@ -2839,6 +2844,7 @@ private void inflateAndAddDefaultIconView() {
28392844
(ImageView)
28402845
LayoutInflater.from(getContext())
28412846
.inflate(R.layout.design_layout_tab_icon, this, false);
2847+
iconView.addOnLayoutChangeListener(updateBadgeBoundsOnLayoutChangeListener);
28422848
addView(iconView, 0);
28432849
}
28442850

@@ -2847,6 +2853,7 @@ private void inflateAndAddDefaultTextView() {
28472853
(TextView)
28482854
LayoutInflater.from(getContext())
28492855
.inflate(R.layout.design_layout_tab_text, this, false);
2856+
textView.addOnLayoutChangeListener(updateBadgeBoundsOnLayoutChangeListener);
28502857
addView(textView);
28512858
}
28522859

@@ -2881,30 +2888,6 @@ private void removeBadge() {
28812888
badgeDrawable = null;
28822889
}
28832890

2884-
private void addOnLayoutChangeListener(@Nullable final View view) {
2885-
if (view == null) {
2886-
return;
2887-
}
2888-
view.addOnLayoutChangeListener(
2889-
new OnLayoutChangeListener() {
2890-
@Override
2891-
public void onLayoutChange(
2892-
View v,
2893-
int left,
2894-
int top,
2895-
int right,
2896-
int bottom,
2897-
int oldLeft,
2898-
int oldTop,
2899-
int oldRight,
2900-
int oldBottom) {
2901-
if (view.getVisibility() == VISIBLE) {
2902-
tryUpdateBadgeDrawableBounds(view);
2903-
}
2904-
}
2905-
});
2906-
}
2907-
29082891
private void tryUpdateBadgeAnchor() {
29092892
if (!hasBadgeDrawable()) {
29102893
return;

0 commit comments

Comments
 (0)