Skip to content

Commit

Permalink
[NavigationBar] Modify test usage to use icon container so we don't h…
Browse files Browse the repository at this point in the history
…ave to special case the existence of the icon container

PiperOrigin-RevId: 627837170
  • Loading branch information
imhappi authored and leticiarossi committed Apr 26, 2024
1 parent cc125d9 commit cf143d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public abstract class NavigationBarItemView extends FrameLayout implements MenuV
private int labelVisibilityMode;
private boolean isShifting;

@Nullable private final FrameLayout iconContainer;
@Nullable private final View activeIndicatorView;
@NonNull private final FrameLayout iconContainer;
@NonNull private final View activeIndicatorView;
private final ImageView icon;
private final ViewGroup labelGroup;
private final TextView smallLabel;
Expand Down Expand Up @@ -239,17 +239,6 @@ void clear() {
this.initialized = false;
}

/**
* If this item's layout contains a container which holds the icon and active indicator, return
* the container. Otherwise, return the icon image view.
*
* <p>This is needed for clients who subclass this view and set their own item layout resource
* which might not container an icon container or active indicator view.
*/
private View getIconOrContainer() {
return iconContainer != null ? iconContainer : icon;
}

public void setItemPosition(int position) {
itemPosition = position;
}
Expand Down Expand Up @@ -339,9 +328,7 @@ private void updateActiveIndicatorTransform() {
*/
private void setActiveIndicatorProgress(
@FloatRange(from = 0F, to = 1F) float progress, float target) {
if (activeIndicatorView != null) {
activeIndicatorTransform.updateForProgress(progress, target, activeIndicatorView);
}
activeIndicatorTransform.updateForProgress(progress, target, activeIndicatorView);
activeIndicatorProgress = progress;
}

Expand Down Expand Up @@ -409,12 +396,12 @@ public void setChecked(boolean checked) {
if (checked) {
// Show icon and large label
setViewTopMarginAndGravity(
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
updateViewPaddingBottom(labelGroup, itemPaddingBottom);
largeLabel.setVisibility(VISIBLE);
} else {
// Show icon
setViewTopMarginAndGravity(getIconOrContainer(), itemPaddingTop, Gravity.CENTER);
setViewTopMarginAndGravity(iconContainer, itemPaddingTop, Gravity.CENTER);
updateViewPaddingBottom(labelGroup, 0);
largeLabel.setVisibility(INVISIBLE);
}
Expand All @@ -424,15 +411,15 @@ public void setChecked(boolean checked) {
if (checked) {
// Show icon and large label
setViewTopMarginAndGravity(
getIconOrContainer(),
iconContainer,
(int) (itemPaddingTop + shiftAmount),
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
setViewScaleValues(largeLabel, 1f, 1f, VISIBLE);
setViewScaleValues(smallLabel, scaleUpFactor, scaleUpFactor, INVISIBLE);
} else {
// Show icon and small label
setViewTopMarginAndGravity(
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
setViewScaleValues(largeLabel, scaleDownFactor, scaleDownFactor, INVISIBLE);
setViewScaleValues(smallLabel, 1f, 1f, VISIBLE);
}
Expand All @@ -443,12 +430,12 @@ public void setChecked(boolean checked) {
if (checked) {
// Show icon and large label
setViewTopMarginAndGravity(
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
updateViewPaddingBottom(labelGroup, itemPaddingBottom);
largeLabel.setVisibility(VISIBLE);
} else {
// Show icon only
setViewTopMarginAndGravity(getIconOrContainer(), itemPaddingTop, Gravity.CENTER);
setViewTopMarginAndGravity(iconContainer, itemPaddingTop, Gravity.CENTER);
updateViewPaddingBottom(labelGroup, 0);
largeLabel.setVisibility(INVISIBLE);
}
Expand All @@ -460,23 +447,23 @@ public void setChecked(boolean checked) {
if (checked) {
// Show icon and large label
setViewTopMarginAndGravity(
getIconOrContainer(),
iconContainer,
(int) (itemPaddingTop + shiftAmount),
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
setViewScaleValues(largeLabel, 1f, 1f, VISIBLE);
setViewScaleValues(smallLabel, scaleUpFactor, scaleUpFactor, INVISIBLE);
} else {
// Show icon and small label
setViewTopMarginAndGravity(
getIconOrContainer(), itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
iconContainer, itemPaddingTop, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
setViewScaleValues(largeLabel, scaleDownFactor, scaleDownFactor, INVISIBLE);
setViewScaleValues(smallLabel, 1f, 1f, VISIBLE);
}
break;

case NavigationBarView.LABEL_VISIBILITY_UNLABELED:
// Show icon only
setViewTopMarginAndGravity(getIconOrContainer(), itemPaddingTop, Gravity.CENTER);
setViewTopMarginAndGravity(iconContainer, itemPaddingTop, Gravity.CENTER);
largeLabel.setVisibility(GONE);
smallLabel.setVisibility(GONE);
break;
Expand Down Expand Up @@ -716,7 +703,6 @@ private void refreshItemBackground() {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP
&& activeIndicatorEnabled
&& getActiveIndicatorDrawable() != null
&& iconContainer != null
&& maskDrawable != null) {

// Remove the default focus highlight that highlights the entire view and rely on the
Expand All @@ -733,13 +719,9 @@ && getActiveIndicatorDrawable() != null
itemBackgroundDrawable = createItemBackgroundCompat(itemRippleColor);
}
}
// Check that this item includes an icon container. If a NavigationBarView's subclass supplies
// a custom item layout, this can be null.
if (iconContainer != null) {
// Remove any padding to avoid the active indicator from from being clipped
iconContainer.setPadding(0, 0, 0, 0);
iconContainer.setForeground(iconContainerRippleDrawable);
}
// Remove any padding to avoid the active indicator from from being clipped
iconContainer.setPadding(0, 0, 0, 0);
iconContainer.setForeground(iconContainerRippleDrawable);
ViewCompat.setBackground(this, itemBackgroundDrawable);
if (VERSION.SDK_INT >= VERSION_CODES.O) {
setDefaultFocusHighlightEnabled(defaultHighlightEnabled);
Expand Down Expand Up @@ -800,10 +782,8 @@ public void setActiveIndicatorLabelPadding(int activeIndicatorLabelPadding) {
public void setActiveIndicatorEnabled(boolean enabled) {
this.activeIndicatorEnabled = enabled;
refreshItemBackground();
if (activeIndicatorView != null) {
activeIndicatorView.setVisibility(enabled ? View.VISIBLE : View.GONE);
requestLayout();
}
activeIndicatorView.setVisibility(enabled ? View.VISIBLE : View.GONE);
requestLayout();
}

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

Expand Down Expand Up @@ -876,27 +856,19 @@ public void setActiveIndicatorMarginHorizontal(@Px int marginHorizontal) {
/** Get the drawable used as the active indicator. */
@Nullable
public Drawable getActiveIndicatorDrawable() {
if (activeIndicatorView == null) {
return null;
}

return activeIndicatorView.getBackground();
}

/** Set the drawable to be used as the active indicator. */
public void setActiveIndicatorDrawable(@Nullable Drawable activeIndicatorDrawable) {
if (activeIndicatorView == null) {
return;
}

activeIndicatorView.setBackgroundDrawable(activeIndicatorDrawable);
refreshItemBackground();
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// Pass touch events through to the icon container so the active indicator ripple can be shown.
if (iconContainer != null && activeIndicatorEnabled) {
if (activeIndicatorEnabled) {
iconContainer.dispatchTouchEvent(ev);
}
return super.dispatchTouchEvent(ev);
Expand Down Expand Up @@ -987,16 +959,16 @@ private int getSuggestedIconWidth() {

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

private int getSuggestedIconHeight() {
LayoutParams iconContainerParams = (LayoutParams) getIconOrContainer().getLayoutParams();
LayoutParams iconContainerParams = (LayoutParams) iconContainer.getLayoutParams();
return iconContainerParams.topMargin
+ getIconOrContainer().getMeasuredHeight();
+ iconContainer.getMeasuredHeight();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@
~ limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
<FrameLayout
android:id="@id/navigation_bar_item_icon_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/mtrl_navigation_bar_item_default_margin"
android:layout_marginBottom="@dimen/mtrl_navigation_bar_item_default_margin"
android:duplicateParentState="true">
<View
android:id="@id/navigation_bar_item_active_indicator_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center" />
<ImageView
android:id="@id/navigation_bar_item_icon_view"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="@dimen/mtrl_navigation_bar_item_default_margin"
android:layout_marginBottom="@dimen/mtrl_navigation_bar_item_default_margin"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
android:duplicateParentState="true"/>
</FrameLayout>
<com.google.android.material.internal.BaselineLayout
android:id="@id/navigation_bar_item_labels_group"
android:layout_width="wrap_content"
Expand Down

0 comments on commit cf143d0

Please sign in to comment.