Skip to content

Commit cf143d0

Browse files
imhappileticiarossi
authored andcommitted
[NavigationBar] Modify test usage to use icon container so we don't have to special case the existence of the icon container
PiperOrigin-RevId: 627837170
1 parent cc125d9 commit cf143d0

File tree

2 files changed

+37
-53
lines changed

2 files changed

+37
-53
lines changed

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

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public abstract class NavigationBarItemView extends FrameLayout implements MenuV
9595
private int labelVisibilityMode;
9696
private boolean isShifting;
9797

98-
@Nullable private final FrameLayout iconContainer;
99-
@Nullable private final View activeIndicatorView;
98+
@NonNull private final FrameLayout iconContainer;
99+
@NonNull private final View activeIndicatorView;
100100
private final ImageView icon;
101101
private final ViewGroup labelGroup;
102102
private final TextView smallLabel;
@@ -239,17 +239,6 @@ void clear() {
239239
this.initialized = false;
240240
}
241241

242-
/**
243-
* If this item's layout contains a container which holds the icon and active indicator, return
244-
* the container. Otherwise, return the icon image view.
245-
*
246-
* <p>This is needed for clients who subclass this view and set their own item layout resource
247-
* which might not container an icon container or active indicator view.
248-
*/
249-
private View getIconOrContainer() {
250-
return iconContainer != null ? iconContainer : icon;
251-
}
252-
253242
public void setItemPosition(int position) {
254243
itemPosition = position;
255244
}
@@ -339,9 +328,7 @@ private void updateActiveIndicatorTransform() {
339328
*/
340329
private void setActiveIndicatorProgress(
341330
@FloatRange(from = 0F, to = 1F) float progress, float target) {
342-
if (activeIndicatorView != null) {
343-
activeIndicatorTransform.updateForProgress(progress, target, activeIndicatorView);
344-
}
331+
activeIndicatorTransform.updateForProgress(progress, target, activeIndicatorView);
345332
activeIndicatorProgress = progress;
346333
}
347334

@@ -409,12 +396,12 @@ public void setChecked(boolean checked) {
409396
if (checked) {
410397
// Show icon and large label
411398
setViewTopMarginAndGravity(
412-
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
399+
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
413400
updateViewPaddingBottom(labelGroup, itemPaddingBottom);
414401
largeLabel.setVisibility(VISIBLE);
415402
} else {
416403
// Show icon
417-
setViewTopMarginAndGravity(getIconOrContainer(), itemPaddingTop, Gravity.CENTER);
404+
setViewTopMarginAndGravity(iconContainer, itemPaddingTop, Gravity.CENTER);
418405
updateViewPaddingBottom(labelGroup, 0);
419406
largeLabel.setVisibility(INVISIBLE);
420407
}
@@ -424,15 +411,15 @@ public void setChecked(boolean checked) {
424411
if (checked) {
425412
// Show icon and large label
426413
setViewTopMarginAndGravity(
427-
getIconOrContainer(),
414+
iconContainer,
428415
(int) (itemPaddingTop + shiftAmount),
429416
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
430417
setViewScaleValues(largeLabel, 1f, 1f, VISIBLE);
431418
setViewScaleValues(smallLabel, scaleUpFactor, scaleUpFactor, INVISIBLE);
432419
} else {
433420
// Show icon and small label
434421
setViewTopMarginAndGravity(
435-
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
422+
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
436423
setViewScaleValues(largeLabel, scaleDownFactor, scaleDownFactor, INVISIBLE);
437424
setViewScaleValues(smallLabel, 1f, 1f, VISIBLE);
438425
}
@@ -443,12 +430,12 @@ public void setChecked(boolean checked) {
443430
if (checked) {
444431
// Show icon and large label
445432
setViewTopMarginAndGravity(
446-
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
433+
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
447434
updateViewPaddingBottom(labelGroup, itemPaddingBottom);
448435
largeLabel.setVisibility(VISIBLE);
449436
} else {
450437
// Show icon only
451-
setViewTopMarginAndGravity(getIconOrContainer(), itemPaddingTop, Gravity.CENTER);
438+
setViewTopMarginAndGravity(iconContainer, itemPaddingTop, Gravity.CENTER);
452439
updateViewPaddingBottom(labelGroup, 0);
453440
largeLabel.setVisibility(INVISIBLE);
454441
}
@@ -460,23 +447,23 @@ public void setChecked(boolean checked) {
460447
if (checked) {
461448
// Show icon and large label
462449
setViewTopMarginAndGravity(
463-
getIconOrContainer(),
450+
iconContainer,
464451
(int) (itemPaddingTop + shiftAmount),
465452
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
466453
setViewScaleValues(largeLabel, 1f, 1f, VISIBLE);
467454
setViewScaleValues(smallLabel, scaleUpFactor, scaleUpFactor, INVISIBLE);
468455
} else {
469456
// Show icon and small label
470457
setViewTopMarginAndGravity(
471-
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
458+
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
472459
setViewScaleValues(largeLabel, scaleDownFactor, scaleDownFactor, INVISIBLE);
473460
setViewScaleValues(smallLabel, 1f, 1f, VISIBLE);
474461
}
475462
break;
476463

477464
case NavigationBarView.LABEL_VISIBILITY_UNLABELED:
478465
// Show icon only
479-
setViewTopMarginAndGravity(getIconOrContainer(), itemPaddingTop, Gravity.CENTER);
466+
setViewTopMarginAndGravity(iconContainer, itemPaddingTop, Gravity.CENTER);
480467
largeLabel.setVisibility(GONE);
481468
smallLabel.setVisibility(GONE);
482469
break;
@@ -716,7 +703,6 @@ private void refreshItemBackground() {
716703
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP
717704
&& activeIndicatorEnabled
718705
&& getActiveIndicatorDrawable() != null
719-
&& iconContainer != null
720706
&& maskDrawable != null) {
721707

722708
// Remove the default focus highlight that highlights the entire view and rely on the
@@ -733,13 +719,9 @@ && getActiveIndicatorDrawable() != null
733719
itemBackgroundDrawable = createItemBackgroundCompat(itemRippleColor);
734720
}
735721
}
736-
// Check that this item includes an icon container. If a NavigationBarView's subclass supplies
737-
// a custom item layout, this can be null.
738-
if (iconContainer != null) {
739-
// Remove any padding to avoid the active indicator from from being clipped
740-
iconContainer.setPadding(0, 0, 0, 0);
741-
iconContainer.setForeground(iconContainerRippleDrawable);
742-
}
722+
// Remove any padding to avoid the active indicator from from being clipped
723+
iconContainer.setPadding(0, 0, 0, 0);
724+
iconContainer.setForeground(iconContainerRippleDrawable);
743725
ViewCompat.setBackground(this, itemBackgroundDrawable);
744726
if (VERSION.SDK_INT >= VERSION_CODES.O) {
745727
setDefaultFocusHighlightEnabled(defaultHighlightEnabled);
@@ -800,10 +782,8 @@ public void setActiveIndicatorLabelPadding(int activeIndicatorLabelPadding) {
800782
public void setActiveIndicatorEnabled(boolean enabled) {
801783
this.activeIndicatorEnabled = enabled;
802784
refreshItemBackground();
803-
if (activeIndicatorView != null) {
804-
activeIndicatorView.setVisibility(enabled ? View.VISIBLE : View.GONE);
805-
requestLayout();
806-
}
785+
activeIndicatorView.setVisibility(enabled ? View.VISIBLE : View.GONE);
786+
requestLayout();
807787
}
808788

809789
/**
@@ -829,7 +809,7 @@ public void setActiveIndicatorWidth(int width) {
829809
private void updateActiveIndicatorLayoutParams(int availableWidth) {
830810
// Set width to the min of either the desired indicator width or the available width minus
831811
// a horizontal margin.
832-
if (activeIndicatorView == null || availableWidth <= 0) {
812+
if (availableWidth <= 0) {
833813
return;
834814
}
835815

@@ -876,27 +856,19 @@ public void setActiveIndicatorMarginHorizontal(@Px int marginHorizontal) {
876856
/** Get the drawable used as the active indicator. */
877857
@Nullable
878858
public Drawable getActiveIndicatorDrawable() {
879-
if (activeIndicatorView == null) {
880-
return null;
881-
}
882-
883859
return activeIndicatorView.getBackground();
884860
}
885861

886862
/** Set the drawable to be used as the active indicator. */
887863
public void setActiveIndicatorDrawable(@Nullable Drawable activeIndicatorDrawable) {
888-
if (activeIndicatorView == null) {
889-
return;
890-
}
891-
892864
activeIndicatorView.setBackgroundDrawable(activeIndicatorDrawable);
893865
refreshItemBackground();
894866
}
895867

896868
@Override
897869
public boolean dispatchTouchEvent(MotionEvent ev) {
898870
// Pass touch events through to the icon container so the active indicator ripple can be shown.
899-
if (iconContainer != null && activeIndicatorEnabled) {
871+
if (activeIndicatorEnabled) {
900872
iconContainer.dispatchTouchEvent(ev);
901873
}
902874
return super.dispatchTouchEvent(ev);
@@ -987,16 +959,16 @@ private int getSuggestedIconWidth() {
987959

988960
// Account for the fact that the badge may fit within the left or right margin. Give the same
989961
// space of either side so that icon position does not move if badge gravity is changed.
990-
LayoutParams iconContainerParams = (LayoutParams) getIconOrContainer().getLayoutParams();
962+
LayoutParams iconContainerParams = (LayoutParams) iconContainer.getLayoutParams();
991963
return max(badgeWidth, iconContainerParams.leftMargin)
992964
+ icon.getMeasuredWidth()
993965
+ max(badgeWidth, iconContainerParams.rightMargin);
994966
}
995967

996968
private int getSuggestedIconHeight() {
997-
LayoutParams iconContainerParams = (LayoutParams) getIconOrContainer().getLayoutParams();
969+
LayoutParams iconContainerParams = (LayoutParams) iconContainer.getLayoutParams();
998970
return iconContainerParams.topMargin
999-
+ getIconOrContainer().getMeasuredHeight();
971+
+ iconContainer.getMeasuredHeight();
1000972
}
1001973

1002974
/**

lib/javatests/com/google/android/material/navigation/res/layout/test_navigation_bar_item_layout.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,27 @@
1515
~ limitations under the License.
1616
-->
1717
<merge xmlns:android="http://schemas.android.com/apk/res/android">
18-
<ImageView
18+
<FrameLayout
19+
android:id="@id/navigation_bar_item_icon_container"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_gravity="center_horizontal"
23+
android:layout_marginTop="@dimen/mtrl_navigation_bar_item_default_margin"
24+
android:layout_marginBottom="@dimen/mtrl_navigation_bar_item_default_margin"
25+
android:duplicateParentState="true">
26+
<View
27+
android:id="@id/navigation_bar_item_active_indicator_view"
28+
android:layout_width="0dp"
29+
android:layout_height="0dp"
30+
android:layout_gravity="center" />
31+
<ImageView
1932
android:id="@id/navigation_bar_item_icon_view"
2033
android:layout_width="24dp"
2134
android:layout_height="24dp"
22-
android:layout_marginTop="@dimen/mtrl_navigation_bar_item_default_margin"
23-
android:layout_marginBottom="@dimen/mtrl_navigation_bar_item_default_margin"
2435
android:layout_gravity="center_horizontal"
2536
android:contentDescription="@null"
2637
android:duplicateParentState="true"/>
38+
</FrameLayout>
2739
<com.google.android.material.internal.BaselineLayout
2840
android:id="@id/navigation_bar_item_labels_group"
2941
android:layout_width="wrap_content"

0 commit comments

Comments
 (0)