Skip to content

Commit 619c95c

Browse files
hunterstichleticiarossi
authored andcommitted
[NavigationBar] Fixed menu visibility changes causing active indicator flashing.
PiperOrigin-RevId: 393365926
1 parent 27e7b00 commit 619c95c

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

lib/java/com/google/android/material/navigation/NavigationBarItemView.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public abstract class NavigationBarItemView extends FrameLayout implements MenuV
7474
private static final int INVALID_ITEM_POSITION = -1;
7575
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
7676

77+
private boolean initialized = false;
7778
private int itemPaddingTop;
7879
private int itemPaddingBottom;
7980
private float shiftAmount;
@@ -198,6 +199,22 @@ public void initialize(@NonNull MenuItemImpl itemData, int menuType) {
198199
TooltipCompat.setTooltipText(this, tooltipText);
199200
}
200201
setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
202+
this.initialized = true;
203+
}
204+
205+
/**
206+
* Remove state so this View can be reused.
207+
*
208+
* Item Views are held in a pool and reused when the number of menu items to be shown changes.
209+
* This will be called when this View is released from the pool.
210+
*
211+
* @see NavigationBarMenuView#buildMenuView()
212+
*/
213+
void clear() {
214+
this.removeBadge();
215+
this.itemData = null;
216+
this.activeIndicatorProgress = 0;
217+
this.initialized = false;
201218
}
202219

203220
/**
@@ -305,7 +322,9 @@ private void setActiveIndicatorProgress(
305322
/** If the active indicator is enabled, animate from it's current state to it's new state. */
306323
private void maybeAnimateActiveIndicatorToProgress(
307324
@FloatRange(from = 0F, to = 1F) final float newProgress) {
308-
if (!activeIndicatorEnabled) {
325+
// If the active indicator is disabled or this view is in the process of being initialized,
326+
// jump the active indicator to it's final state.
327+
if (!activeIndicatorEnabled || !initialized) {
309328
setActiveIndicatorProgress(newProgress, newProgress);
310329
return;
311330
}
@@ -628,14 +647,18 @@ public void setItemBackground(@Nullable Drawable background) {
628647
* Set the padding applied to the icon/active indicator container from the top of the item view.
629648
*/
630649
public void setItemPaddingTop(int paddingTop) {
631-
this.itemPaddingTop = paddingTop;
632-
refreshChecked();
650+
if (this.itemPaddingTop != paddingTop) {
651+
this.itemPaddingTop = paddingTop;
652+
refreshChecked();
653+
}
633654
}
634655

635656
/** Set the padding applied to the labels from the bottom of the item view. */
636657
public void setItemPaddingBottom(int paddingBottom) {
637-
this.itemPaddingBottom = paddingBottom;
638-
refreshChecked();
658+
if (this.itemPaddingBottom != paddingBottom) {
659+
this.itemPaddingBottom = paddingBottom;
660+
refreshChecked();
661+
}
639662
}
640663

641664
/** Set whether or not this item should show an active indicator when checked. */

lib/java/com/google/android/material/navigation/NavigationBarMenuView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ public void buildMenuView() {
630630
for (NavigationBarItemView item : buttons) {
631631
if (item != null) {
632632
itemPool.release(item);
633-
item.removeBadge();
633+
item.clear();
634634
}
635635
}
636636
}

0 commit comments

Comments
 (0)