Skip to content

Commit a2ce1dd

Browse files
anyercastilloafohrman
authored andcommitted
[BottomNavigation] Fix hardcoded number of children
Resolves #2923 GIT_ORIGIN_REV_ID=54a68268367a3f0efda6928e10b5f9c299502121 Co-authored-by: imhappi PiperOrigin-RevId: 475346937
1 parent 7bc5689 commit a2ce1dd

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

lib/java/com/google/android/material/bottomnavigation/BottomNavigationMenuView.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import androidx.core.view.ViewCompat;
3333
import com.google.android.material.navigation.NavigationBarItemView;
3434
import com.google.android.material.navigation.NavigationBarMenuView;
35+
import java.util.ArrayList;
36+
import java.util.List;
3537

3638
/** @hide For internal use only. */
3739
@RestrictTo(LIBRARY_GROUP)
@@ -42,7 +44,7 @@ public class BottomNavigationMenuView extends NavigationBarMenuView {
4244
private final int activeItemMinWidth;
4345

4446
private boolean itemHorizontalTranslationEnabled;
45-
private int[] tempChildWidths;
47+
private final List<Integer> tempChildWidths = new ArrayList<>();
4648

4749
public BottomNavigationMenuView(@NonNull Context context) {
4850
super(context);
@@ -63,7 +65,6 @@ public BottomNavigationMenuView(@NonNull Context context) {
6365
activeItemMinWidth =
6466
res.getDimensionPixelSize(R.dimen.design_bottom_navigation_active_item_min_width);
6567

66-
tempChildWidths = new int[BottomNavigationView.MAX_ITEM_COUNT];
6768
}
6869

6970
@Override
@@ -74,6 +75,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
7475
final int visibleCount = menu.getVisibleItems().size();
7576
// Use total item counts to measure children
7677
final int totalCount = getChildCount();
78+
tempChildWidths.clear();
7779

7880
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
7981
final int heightSpec = MeasureSpec.makeMeasureSpec(parentHeight, MeasureSpec.EXACTLY);
@@ -99,33 +101,33 @@ && isItemHorizontalTranslationEnabled()) {
99101
int extra = width - activeWidth - inactiveWidth * inactiveCount;
100102

101103
for (int i = 0; i < totalCount; i++) {
104+
int tempChildWidth = 0;
102105
if (getChildAt(i).getVisibility() != View.GONE) {
103-
tempChildWidths[i] = (i == getSelectedItemPosition()) ? activeWidth : inactiveWidth;
106+
tempChildWidth = (i == getSelectedItemPosition()) ? activeWidth : inactiveWidth;
104107
// Account for integer division which sometimes leaves some extra pixel spaces.
105108
// e.g. If the nav was 10px wide, and 3 children were measured to be 3px-3px-3px, there
106109
// would be a 1px gap somewhere, which this fills in.
107110
if (extra > 0) {
108-
tempChildWidths[i]++;
111+
tempChildWidth++;
109112
extra--;
110113
}
111-
} else {
112-
tempChildWidths[i] = 0;
113114
}
115+
tempChildWidths.add(tempChildWidth);
114116
}
115117
} else {
116118
final int maxAvailable = width / (visibleCount == 0 ? 1 : visibleCount);
117119
final int childWidth = Math.min(maxAvailable, activeItemMaxWidth);
118120
int extra = width - childWidth * visibleCount;
119121
for (int i = 0; i < totalCount; i++) {
122+
int tempChildWidth = 0;
120123
if (getChildAt(i).getVisibility() != View.GONE) {
121-
tempChildWidths[i] = childWidth;
124+
tempChildWidth = childWidth;
122125
if (extra > 0) {
123-
tempChildWidths[i]++;
126+
tempChildWidth++;
124127
extra--;
125128
}
126-
} else {
127-
tempChildWidths[i] = 0;
128129
}
130+
tempChildWidths.add(tempChildWidth);
129131
}
130132
}
131133

@@ -136,7 +138,7 @@ && isItemHorizontalTranslationEnabled()) {
136138
continue;
137139
}
138140
child.measure(
139-
MeasureSpec.makeMeasureSpec(tempChildWidths[i], MeasureSpec.EXACTLY), heightSpec);
141+
MeasureSpec.makeMeasureSpec(tempChildWidths.get(i), MeasureSpec.EXACTLY), heightSpec);
140142
ViewGroup.LayoutParams params = child.getLayoutParams();
141143
params.width = child.getMeasuredWidth();
142144
totalWidth += child.getMeasuredWidth();

lib/java/com/google/android/material/bottomnavigation/BottomNavigationView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
* </pre>
8989
*/
9090
public class BottomNavigationView extends NavigationBarView {
91-
static final int MAX_ITEM_COUNT = 5;
91+
private static final int MAX_ITEM_COUNT = 5;
9292

9393
public BottomNavigationView(@NonNull Context context) {
9494
this(context, null);

0 commit comments

Comments
 (0)