Skip to content
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
35 changes: 9 additions & 26 deletions lib/java/com/google/android/material/tabs/TabLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,13 @@ public final class TabView extends LinearLayout {

private int defaultMaxLines = 2;

private final OnLayoutChangeListener updateBadgeBoundsOnLayoutChangeListener =
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
if (v.getVisibility() == VISIBLE) {
tryUpdateBadgeDrawableBounds(v);
}
};

public TabView(@NonNull Context context) {
super(context);
updateBackgroundDrawable(context);
Expand Down Expand Up @@ -2812,8 +2819,6 @@ final void updateTab() {
updateTextAndIcon(this.textView, this.iconView, /* addDefaultMargins= */ true);

tryUpdateBadgeAnchor();
addOnLayoutChangeListener(iconView);
addOnLayoutChangeListener(textView);
} else {
// Else, we'll see if there is a TextView or ImageView present and update them
if (customTextView != null || customIconView != null) {
Expand All @@ -2839,6 +2844,7 @@ private void inflateAndAddDefaultIconView() {
(ImageView)
LayoutInflater.from(getContext())
.inflate(R.layout.design_layout_tab_icon, this, false);
iconView.addOnLayoutChangeListener(updateBadgeBoundsOnLayoutChangeListener);
addView(iconView, 0);
}

Expand All @@ -2847,6 +2853,7 @@ private void inflateAndAddDefaultTextView() {
(TextView)
LayoutInflater.from(getContext())
.inflate(R.layout.design_layout_tab_text, this, false);
textView.addOnLayoutChangeListener(updateBadgeBoundsOnLayoutChangeListener);
addView(textView);
}

Expand Down Expand Up @@ -2881,30 +2888,6 @@ private void removeBadge() {
badgeDrawable = null;
}

private void addOnLayoutChangeListener(@Nullable final View view) {
if (view == null) {
return;
}
view.addOnLayoutChangeListener(
new OnLayoutChangeListener() {
@Override
public void onLayoutChange(
View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
if (view.getVisibility() == VISIBLE) {
tryUpdateBadgeDrawableBounds(view);
}
}
});
}

private void tryUpdateBadgeAnchor() {
if (!hasBadgeDrawable()) {
return;
Expand Down