/
NavigationBarMenuView.java
907 lines (821 loc) · 28.7 KB
/
NavigationBarMenuView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.navigation;
import com.google.android.material.R;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.view.menu.MenuItemImpl;
import androidx.appcompat.view.menu.MenuView;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.StyleRes;
import androidx.core.util.Pools;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat;
import androidx.transition.AutoTransition;
import androidx.transition.TransitionManager;
import androidx.transition.TransitionSet;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.internal.TextScale;
import com.google.android.material.motion.MotionUtils;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import java.util.HashSet;
/**
* Provides a view that will be use to render a menu view inside a {@link NavigationBarView}.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public abstract class NavigationBarMenuView extends ViewGroup implements MenuView {
private static final int ITEM_POOL_SIZE = 5;
private static final int NO_PADDING = -1;
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
private static final int[] DISABLED_STATE_SET = {-android.R.attr.state_enabled};
@NonNull private final TransitionSet set;
@NonNull private final OnClickListener onClickListener;
private final Pools.Pool<NavigationBarItemView> itemPool =
new Pools.SynchronizedPool<>(ITEM_POOL_SIZE);
@NonNull
private final SparseArray<OnTouchListener> onTouchListeners = new SparseArray<>(ITEM_POOL_SIZE);
@NavigationBarView.LabelVisibility private int labelVisibilityMode;
@Nullable private NavigationBarItemView[] buttons;
private int selectedItemId = 0;
private int selectedItemPosition = 0;
@Nullable private ColorStateList itemIconTint;
@Dimension private int itemIconSize;
private ColorStateList itemTextColorFromUser;
@Nullable private final ColorStateList itemTextColorDefault;
@StyleRes private int itemTextAppearanceInactive;
@StyleRes private int itemTextAppearanceActive;
private Drawable itemBackground;
private int itemBackgroundRes;
@NonNull private final SparseArray<BadgeDrawable> badgeDrawables =
new SparseArray<>(ITEM_POOL_SIZE);
private int itemPaddingTop = NO_PADDING;
private int itemPaddingBottom = NO_PADDING;
private boolean itemActiveIndicatorEnabled;
private int itemActiveIndicatorWidth;
private int itemActiveIndicatorHeight;
private int itemActiveIndicatorMarginHorizontal;
private ShapeAppearanceModel itemActiveIndicatorShapeAppearance;
private boolean itemActiveIndicatorResizeable = false;
private ColorStateList itemActiveIndicatorColor;
private NavigationBarPresenter presenter;
private MenuBuilder menu;
public NavigationBarMenuView(@NonNull Context context) {
super(context);
itemTextColorDefault = createDefaultColorStateList(android.R.attr.textColorSecondary);
set = new AutoTransition();
set.setOrdering(TransitionSet.ORDERING_TOGETHER);
set.setDuration(
MotionUtils.resolveThemeDuration(
getContext(),
R.attr.motionDurationLong1,
getResources().getInteger(R.integer.material_motion_duration_long_1)));
set.setInterpolator(
MotionUtils.resolveThemeInterpolator(
getContext(),
R.attr.motionEasingStandard,
AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
set.addTransition(new TextScale());
onClickListener =
new OnClickListener() {
@Override
public void onClick(View v) {
final NavigationBarItemView itemView = (NavigationBarItemView) v;
MenuItem item = itemView.getItemData();
if (!menu.performItemAction(item, presenter, 0)) {
item.setChecked(true);
}
}
};
ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
@Override
public void initialize(@NonNull MenuBuilder menu) {
this.menu = menu;
}
@Override
public int getWindowAnimations() {
return 0;
}
@Override
public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
AccessibilityNodeInfoCompat infoCompat = AccessibilityNodeInfoCompat.wrap(info);
infoCompat.setCollectionInfo(
CollectionInfoCompat.obtain(
/* rowCount= */ 1,
/* columnCount= */ menu.getVisibleItems().size(),
/* hierarchical= */ false,
/* selectionMode = */ CollectionInfoCompat.SELECTION_MODE_SINGLE));
}
/**
* Sets the tint which is applied to the menu items' icons.
*
* @param tint the tint to apply
*/
public void setIconTintList(@Nullable ColorStateList tint) {
itemIconTint = tint;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setIconTintList(tint);
}
}
}
/**
* Returns the tint which is applied for the menu item labels.
*
* @return the ColorStateList that is used to tint menu items' icons
*/
@Nullable
public ColorStateList getIconTintList() {
return itemIconTint;
}
/**
* Sets the size to provide for the menu item icons.
*
* <p>For best image resolution, use an icon with the same size set in this method.
*
* @param iconSize the size to provide for the menu item icons in pixels
*/
public void setItemIconSize(@Dimension int iconSize) {
this.itemIconSize = iconSize;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setIconSize(iconSize);
}
}
}
/** Returns the size in pixels provided for the menu item icons. */
@Dimension
public int getItemIconSize() {
return itemIconSize;
}
/**
* Sets the text color to be used for the menu item labels.
*
* @param color the ColorStateList used for menu item labels
*/
public void setItemTextColor(@Nullable ColorStateList color) {
itemTextColorFromUser = color;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setTextColor(color);
}
}
}
/**
* Returns the text color used for menu item labels.
*
* @return the ColorStateList used for menu items labels
*/
@Nullable
public ColorStateList getItemTextColor() {
return itemTextColorFromUser;
}
/**
* Sets the text appearance to be used for inactive menu item labels.
*
* @param textAppearanceRes the text appearance ID used for inactive menu item labels
*/
public void setItemTextAppearanceInactive(@StyleRes int textAppearanceRes) {
this.itemTextAppearanceInactive = textAppearanceRes;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setTextAppearanceInactive(textAppearanceRes);
// Set the text color if the user has set it, since itemTextColorFromUser takes precedence
// over a color set in the text appearance.
if (itemTextColorFromUser != null) {
item.setTextColor(itemTextColorFromUser);
}
}
}
}
/**
* Returns the text appearance used for inactive menu item labels.
*
* @return the text appearance ID used for inactive menu item labels
*/
@StyleRes
public int getItemTextAppearanceInactive() {
return itemTextAppearanceInactive;
}
/**
* Sets the text appearance to be used for the active menu item label.
*
* @param textAppearanceRes the text appearance ID used for the active menu item label
*/
public void setItemTextAppearanceActive(@StyleRes int textAppearanceRes) {
this.itemTextAppearanceActive = textAppearanceRes;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setTextAppearanceActive(textAppearanceRes);
// Set the text color if the user has set it, since itemTextColorFromUser takes precedence
// over a color set in the text appearance.
if (itemTextColorFromUser != null) {
item.setTextColor(itemTextColorFromUser);
}
}
}
}
/**
* Returns the text appearance used for the active menu item label.
*
* @return the text appearance ID used for the active menu item label
*/
@StyleRes
public int getItemTextAppearanceActive() {
return itemTextAppearanceActive;
}
/**
* Sets the resource ID to be used for item backgrounds.
*
* @param background the resource ID of the background
*/
public void setItemBackgroundRes(int background) {
itemBackgroundRes = background;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setItemBackground(background);
}
}
}
/**
* Get the distance from the top of an item's icon/active indicator to the top of the navigation
* bar item.
*/
@Px
public int getItemPaddingTop() {
return itemPaddingTop;
}
/**
* Set the distance from the top of an items icon/active indicator to the top of the navigation
* bar item.
*/
public void setItemPaddingTop(@Px int paddingTop) {
itemPaddingTop = paddingTop;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setItemPaddingTop(paddingTop);
}
}
}
/**
* Get the distance from the bottom of an item's label to the bottom of the navigation bar item.
*/
@Px
public int getItemPaddingBottom() {
return itemPaddingBottom;
}
/**
* Set the distance from the bottom of an item's label to the bottom of the navigation bar item.
*/
public void setItemPaddingBottom(@Px int paddingBottom) {
itemPaddingBottom = paddingBottom;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setItemPaddingBottom(paddingBottom);
}
}
}
/**
* Returns whether or not an active indicator is enabled for the navigation bar.
*
* @return true if the active indicator is enabled.
*/
public boolean getItemActiveIndicatorEnabled() {
return itemActiveIndicatorEnabled;
}
/**
* Set whether or not an active indicator is enabled for the navigation bar.
*
* @param enabled true if an active indicator should be shown.
*/
public void setItemActiveIndicatorEnabled(boolean enabled) {
this.itemActiveIndicatorEnabled = enabled;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorEnabled(enabled);
}
}
}
/**
* Get the width of the selected item's active indicator.
*
* @return The width, in pixels, of the active indicator.
*/
@Px
public int getItemActiveIndicatorWidth() {
return itemActiveIndicatorWidth;
}
/**
* Set the width to be used for the selected item's active indicator.
*
* @param width The width, in pixels, of the active indicator.
*/
public void setItemActiveIndicatorWidth(@Px int width) {
this.itemActiveIndicatorWidth = width;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorWidth(width);
}
}
}
/**
* Get the height of the selected item's active indicator.
*
* @return The height, in pixels, of the active indicator.
*/
@Px
public int getItemActiveIndicatorHeight() {
return itemActiveIndicatorHeight;
}
/**
* Set the height to be used for the selected item's active indicator.
*
* @param height The height, in pixels, of the active indicator.
*/
public void setItemActiveIndicatorHeight(@Px int height) {
this.itemActiveIndicatorHeight = height;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorHeight(height);
}
}
}
/**
* Get the margin that will be maintained at the start and end of the active indicator away from
* the edges of its parent container.
*
* @return The horizontal margin, in pixels.
*/
@Px
public int getItemActiveIndicatorMarginHorizontal() {
return itemActiveIndicatorMarginHorizontal;
}
/**
* Set the horizontal margin that will be maintained at the start and end of the active indicator,
* making sure the indicator remains the given distance from the edge of its parent container.
*
* @param marginHorizontal The horizontal margin, in pixels.
*/
public void setItemActiveIndicatorMarginHorizontal(@Px int marginHorizontal) {
itemActiveIndicatorMarginHorizontal = marginHorizontal;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorMarginHorizontal(marginHorizontal);
}
}
}
/**
* Get the {@link ShapeAppearanceModel} of the active indicator drawable.
*
* @return The {@link ShapeAppearanceModel} of the active indicator drawable.
*/
@Nullable
public ShapeAppearanceModel getItemActiveIndicatorShapeAppearance() {
return itemActiveIndicatorShapeAppearance;
}
/**
* Set the {@link ShapeAppearanceModel} of the active indicator drawable.
*
* @param shapeAppearance The {@link ShapeAppearanceModel} of the active indicator drawable.
*/
public void setItemActiveIndicatorShapeAppearance(
@Nullable ShapeAppearanceModel shapeAppearance) {
this.itemActiveIndicatorShapeAppearance = shapeAppearance;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorDrawable(createItemActiveIndicatorDrawable());
}
}
}
/**
* Get whether the active indicator can be resized.
*/
protected boolean isItemActiveIndicatorResizeable() {
return this.itemActiveIndicatorResizeable;
}
/**
* Set whether the active indicator can be resized. If true, the indicator will automatically
* change size in response to label visibility modes.
*/
protected void setItemActiveIndicatorResizeable(boolean resizeable) {
this.itemActiveIndicatorResizeable = resizeable;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorResizeable(resizeable);
}
}
}
/**
* Get the color of the active indicator drawable.
*
* @return A {@link ColorStateList} used as the color of the active indicator.
*/
@Nullable
public ColorStateList getItemActiveIndicatorColor() {
return itemActiveIndicatorColor;
}
/**
* Set the {@link ColorStateList} of the active indicator drawable.
*
* @param csl The {@link ColorStateList} used as the color of the active indicator.
*/
public void setItemActiveIndicatorColor(@Nullable ColorStateList csl) {
this.itemActiveIndicatorColor = csl;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setActiveIndicatorDrawable(createItemActiveIndicatorDrawable());
}
}
}
/**
* Create a drawable using the {@code itemActiveIndicatorShapeAppearance} and {@code
* itemActiveIndicatorColor} to be used as an item's active indicator.
*
* <p>This method is called once per menu item so each item has a unique drawable instance which
* can be manipulated/animated independently.
*
* @return A drawable to be used as a menu item's active indicator.
*/
@Nullable
private Drawable createItemActiveIndicatorDrawable() {
if (itemActiveIndicatorShapeAppearance != null && itemActiveIndicatorColor != null) {
MaterialShapeDrawable drawable =
new MaterialShapeDrawable(itemActiveIndicatorShapeAppearance);
drawable.setFillColor(itemActiveIndicatorColor);
return drawable;
}
return null;
}
/**
* Returns the resource ID for the background of the menu items.
*
* @return the resource ID for the background
* @deprecated Use {@link #getItemBackground()} instead.
*/
@Deprecated
public int getItemBackgroundRes() {
return itemBackgroundRes;
}
/**
* Sets the drawable to be used for item backgrounds.
*
* @param background the drawable of the background
*/
public void setItemBackground(@Nullable Drawable background) {
itemBackground = background;
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
item.setItemBackground(background);
}
}
}
/**
* Returns the drawable for the background of the menu items.
*
* @return the drawable for the background
*/
@Nullable
public Drawable getItemBackground() {
if (buttons != null && buttons.length > 0) {
// Return button background instead of itemBackground if possible, so that the correct
// drawable is returned if the background is set via #setItemBackgroundRes.
return buttons[0].getBackground();
} else {
return itemBackground;
}
}
/**
* Sets the navigation items' label visibility mode.
*
* <p>The label is either always shown, never shown, or only shown when activated. Also supports
* "auto" mode, which uses the item count to determine whether to show or hide the label.
*
* @param labelVisibilityMode mode which decides whether or not the label should be shown. Can be
* one of {@link NavigationBarView#LABEL_VISIBILITY_AUTO}, {@link
* NavigationBarView#LABEL_VISIBILITY_SELECTED}, {@link
* NavigationBarView#LABEL_VISIBILITY_LABELED}, or {@link
* NavigationBarView#LABEL_VISIBILITY_UNLABELED}
* @see #getLabelVisibilityMode()
*/
public void setLabelVisibilityMode(@NavigationBarView.LabelVisibility int labelVisibilityMode) {
this.labelVisibilityMode = labelVisibilityMode;
}
/**
* Returns the current label visibility mode.
*
* @see #setLabelVisibilityMode(int)
*/
public int getLabelVisibilityMode() {
return labelVisibilityMode;
}
/**
* Sets an {@link android.view.View.OnTouchListener} for the item view associated with the
* provided {@code menuItemId}.
*/
@SuppressLint("ClickableViewAccessibility")
public void setItemOnTouchListener(int menuItemId, @Nullable OnTouchListener onTouchListener) {
if (onTouchListener == null) {
onTouchListeners.remove(menuItemId);
} else {
onTouchListeners.put(menuItemId, onTouchListener);
}
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
if (item.getItemData().getItemId() == menuItemId) {
item.setOnTouchListener(onTouchListener);
}
}
}
}
@Nullable
public ColorStateList createDefaultColorStateList(int baseColorThemeAttr) {
final TypedValue value = new TypedValue();
if (!getContext().getTheme().resolveAttribute(baseColorThemeAttr, value, true)) {
return null;
}
ColorStateList baseColor = AppCompatResources.getColorStateList(getContext(), value.resourceId);
if (!getContext()
.getTheme()
.resolveAttribute(androidx.appcompat.R.attr.colorPrimary, value, true)) {
return null;
}
int colorPrimary = value.data;
int defaultColor = baseColor.getDefaultColor();
return new ColorStateList(
new int[][] {DISABLED_STATE_SET, CHECKED_STATE_SET, EMPTY_STATE_SET},
new int[] {
baseColor.getColorForState(DISABLED_STATE_SET, defaultColor), colorPrimary, defaultColor
});
}
public void setPresenter(@NonNull NavigationBarPresenter presenter) {
this.presenter = presenter;
}
@SuppressLint("ClickableViewAccessibility")
public void buildMenuView() {
removeAllViews();
if (buttons != null) {
for (NavigationBarItemView item : buttons) {
if (item != null) {
itemPool.release(item);
item.clear();
}
}
}
if (menu.size() == 0) {
selectedItemId = 0;
selectedItemPosition = 0;
buttons = null;
return;
}
removeUnusedBadges();
buttons = new NavigationBarItemView[menu.size()];
boolean shifting = isShifting(labelVisibilityMode, menu.getVisibleItems().size());
for (int i = 0; i < menu.size(); i++) {
presenter.setUpdateSuspended(true);
menu.getItem(i).setCheckable(true);
presenter.setUpdateSuspended(false);
NavigationBarItemView child = getNewItem();
buttons[i] = child;
child.setIconTintList(itemIconTint);
child.setIconSize(itemIconSize);
// Set the text color the default, then look for another text color in order of precedence.
child.setTextColor(itemTextColorDefault);
child.setTextAppearanceInactive(itemTextAppearanceInactive);
child.setTextAppearanceActive(itemTextAppearanceActive);
child.setTextColor(itemTextColorFromUser);
if (itemPaddingTop != NO_PADDING) {
child.setItemPaddingTop(itemPaddingTop);
}
if (itemPaddingBottom != NO_PADDING) {
child.setItemPaddingBottom(itemPaddingBottom);
}
child.setActiveIndicatorWidth(itemActiveIndicatorWidth);
child.setActiveIndicatorHeight(itemActiveIndicatorHeight);
child.setActiveIndicatorMarginHorizontal(itemActiveIndicatorMarginHorizontal);
child.setActiveIndicatorDrawable(createItemActiveIndicatorDrawable());
child.setActiveIndicatorResizeable(itemActiveIndicatorResizeable);
child.setActiveIndicatorEnabled(itemActiveIndicatorEnabled);
if (itemBackground != null) {
child.setItemBackground(itemBackground);
} else {
child.setItemBackground(itemBackgroundRes);
}
child.setShifting(shifting);
child.setLabelVisibilityMode(labelVisibilityMode);
MenuItemImpl item = (MenuItemImpl) menu.getItem(i);
child.initialize(item, 0);
child.setItemPosition(i);
int itemId = item.getItemId();
child.setOnTouchListener(onTouchListeners.get(itemId));
child.setOnClickListener(onClickListener);
if (selectedItemId != Menu.NONE && itemId == selectedItemId) {
selectedItemPosition = i;
}
setBadgeIfNeeded(child);
addView(child);
}
selectedItemPosition = Math.min(menu.size() - 1, selectedItemPosition);
menu.getItem(selectedItemPosition).setChecked(true);
}
public void updateMenuView() {
if (menu == null || buttons == null) {
return;
}
final int menuSize = menu.size();
if (menuSize != buttons.length) {
// The size has changed. Rebuild menu view from scratch.
buildMenuView();
return;
}
int previousSelectedId = selectedItemId;
for (int i = 0; i < menuSize; i++) {
MenuItem item = menu.getItem(i);
if (item.isChecked()) {
selectedItemId = item.getItemId();
selectedItemPosition = i;
}
}
if (previousSelectedId != selectedItemId) {
// Note: this has to be called before NavigationBarItemView#initialize().
TransitionManager.beginDelayedTransition(this, set);
}
boolean shifting = isShifting(labelVisibilityMode, menu.getVisibleItems().size());
for (int i = 0; i < menuSize; i++) {
presenter.setUpdateSuspended(true);
buttons[i].setLabelVisibilityMode(labelVisibilityMode);
buttons[i].setShifting(shifting);
buttons[i].initialize((MenuItemImpl) menu.getItem(i), 0);
presenter.setUpdateSuspended(false);
}
}
private NavigationBarItemView getNewItem() {
NavigationBarItemView item = itemPool.acquire();
if (item == null) {
item = createNavigationBarItemView(getContext());
}
return item;
}
public int getSelectedItemId() {
return selectedItemId;
}
protected boolean isShifting(
@NavigationBarView.LabelVisibility int labelVisibilityMode, int childCount) {
return labelVisibilityMode == NavigationBarView.LABEL_VISIBILITY_AUTO
? childCount > 3
: labelVisibilityMode == NavigationBarView.LABEL_VISIBILITY_SELECTED;
}
void tryRestoreSelectedItemId(int itemId) {
final int size = menu.size();
for (int i = 0; i < size; i++) {
MenuItem item = menu.getItem(i);
if (itemId == item.getItemId()) {
selectedItemId = itemId;
selectedItemPosition = i;
item.setChecked(true);
break;
}
}
}
SparseArray<BadgeDrawable> getBadgeDrawables() {
return badgeDrawables;
}
void restoreBadgeDrawables(SparseArray<BadgeDrawable> badgeDrawables) {
for (int i = 0; i < badgeDrawables.size(); i++) {
int key = badgeDrawables.keyAt(i);
if (this.badgeDrawables.indexOfKey(key) < 0) {
// badge doesn't exist yet, restore it
this.badgeDrawables.append(key, badgeDrawables.get(key));
}
}
if (buttons != null) {
for (NavigationBarItemView itemView : buttons) {
itemView.setBadge(this.badgeDrawables.get(itemView.getId()));
}
}
}
@Nullable
public BadgeDrawable getBadge(int menuItemId) {
return badgeDrawables.get(menuItemId);
}
/**
* Creates an instance of {@link BadgeDrawable} if none exists. Initializes (if needed) and
* returns the associated instance of {@link BadgeDrawable}.
*
* @param menuItemId Id of the menu item.
* @return an instance of BadgeDrawable associated with {@code menuItemId}.
*/
BadgeDrawable getOrCreateBadge(int menuItemId) {
validateMenuItemId(menuItemId);
BadgeDrawable badgeDrawable = badgeDrawables.get(menuItemId);
// Create an instance of BadgeDrawable if none were already initialized for this menu item.
if (badgeDrawable == null) {
badgeDrawable = BadgeDrawable.create(getContext());
badgeDrawables.put(menuItemId, badgeDrawable);
}
NavigationBarItemView itemView = findItemView(menuItemId);
if (itemView != null) {
itemView.setBadge(badgeDrawable);
}
return badgeDrawable;
}
void removeBadge(int menuItemId) {
validateMenuItemId(menuItemId);
BadgeDrawable badgeDrawable = badgeDrawables.get(menuItemId);
NavigationBarItemView itemView = findItemView(menuItemId);
if (itemView != null) {
itemView.removeBadge();
}
if (badgeDrawable != null) {
badgeDrawables.remove(menuItemId);
}
}
private void setBadgeIfNeeded(@NonNull NavigationBarItemView child) {
int childId = child.getId();
if (!isValidId(childId)) {
// Child doesn't have a valid id, do not set any BadgeDrawable on the view.
return;
}
BadgeDrawable badgeDrawable = badgeDrawables.get(childId);
if (badgeDrawable != null) {
child.setBadge(badgeDrawable);
}
}
private void removeUnusedBadges() {
HashSet<Integer> activeKeys = new HashSet<>();
// Remove keys from badgeDrawables that don't have a corresponding value in the menu.
for (int i = 0; i < menu.size(); i++) {
activeKeys.add(menu.getItem(i).getItemId());
}
for (int i = 0; i < badgeDrawables.size(); i++) {
int key = badgeDrawables.keyAt(i);
if (!activeKeys.contains(key)) {
badgeDrawables.delete(key);
}
}
}
@Nullable
public NavigationBarItemView findItemView(int menuItemId) {
validateMenuItemId(menuItemId);
if (buttons != null) {
for (NavigationBarItemView itemView : buttons) {
if (itemView.getId() == menuItemId) {
return itemView;
}
}
}
return null;
}
/** Returns reference to newly created {@link NavigationBarItemView}. */
@NonNull
protected abstract NavigationBarItemView createNavigationBarItemView(@NonNull Context context);
protected int getSelectedItemPosition() {
return selectedItemPosition;
}
@Nullable
protected MenuBuilder getMenu() {
return menu;
}
private boolean isValidId(int viewId) {
return viewId != View.NO_ID;
}
private void validateMenuItemId(int viewId) {
if (!isValidId(viewId)) {
throw new IllegalArgumentException(viewId + " is not a valid view id");
}
}
}