Skip to content

Commit

Permalink
[NavigationRail] Moves OnNavigationItem listeners to the NavigationBa…
Browse files Browse the repository at this point in the history
…rView class

to be easier used by developers and other libraries

An example of where the current implementation fails is in Jetpack Navigation component where we rely on setOnNavigationItemSelectedListener and the similar methods in NavigationBarView (setOnItemSelectedListener) are protected.

PiperOrigin-RevId: 367467206
(cherry picked from commit a200a5e)
  • Loading branch information
Material Design Team authored and ymarian committed May 6, 2021
1 parent 2c45dcc commit 0ad4a8d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 42 deletions.
Expand Up @@ -25,7 +25,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
Expand All @@ -36,8 +35,8 @@
import androidx.core.math.MathUtils;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.badge.BadgeDrawable.BadgeGravity;
import com.google.android.material.navigation.NavigationBarView.OnItemSelectedListener;
import com.google.android.material.navigationrail.NavigationRailView;
import com.google.android.material.navigationrail.NavigationRailView.OnNavigationItemSelectedListener;
import io.material.catalog.feature.DemoFragment;

/** A base class that provides a demo screen structure for a single navigation rail demo. */
Expand Down Expand Up @@ -66,7 +65,7 @@ public View onCreateDemoView(
initNavigationRail(getContext(), view);
initNavigationRailDemoControls(view);

OnNavigationItemSelectedListener navigationItemListener =
OnItemSelectedListener navigationItemListener =
item -> {
handleAllNavigationRailSelections(item.getItemId());

Expand Down Expand Up @@ -178,7 +177,7 @@ protected void initNavigationRailDemoControls(View view) {
badgeGravitySpinner.setAdapter(adapter);

badgeGravitySpinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
updateBadgeGravity(
Expand Down Expand Up @@ -214,8 +213,8 @@ private void initRemoveNavItemButton(Button removeNavItemButton) {
});
}

private void setNavigationRailListeners(OnNavigationItemSelectedListener listener) {
navigationRailView.setOnNavigationItemSelectedListener(listener);
private void setNavigationRailListeners(OnItemSelectedListener listener) {
navigationRailView.setOnItemSelectedListener(listener);
}

private void removeNavItemsFromNavigationRails() {
Expand Down
7 changes: 7 additions & 0 deletions docs/components/BottomNavigation.md
Expand Up @@ -102,6 +102,13 @@ That results in:
![Bottom navigation bar with a white background, one selected purple icon and
another unselected icon.](assets/bottomnav/bottom-nav-default.png)

**Note:** We have deprecated the
`BottomNavigationView#setOnNavigationItemSelectedListener` and
`BottomNavigationView#setOnNavigationItemReselectedListener` methods in favor of
the listeners in `NavigationBarView`. This will allow you to share the same
selection code between both `BottomNavigation` and `NavigationRail` view
elements.

### Making bottom navigation accessible

You should set an `android:title` for each of your `menu` items so that screen
Expand Down
5 changes: 4 additions & 1 deletion docs/components/NavigationRail.md
Expand Up @@ -78,7 +78,10 @@ an icon and a text label.
In code:

```kt
NavigationRailView.OnNavigationItemSelectedListener { item ->
// Listeners are defined on the super class NavigationBarView
// to support both NavigationRail and BottomNavigation with the
// same listeners
NavigationBarView.OnNavigationItemSelectedListener { item ->
when(item.itemId) {
R.id.item1 -> {
// Respond to navigation item 1 click
Expand Down
Expand Up @@ -193,6 +193,7 @@ private void addCompatibilityTopDivider(@NonNull Context context) {
* @param listener The listener to notify
* @see #setOnNavigationItemReselectedListener(OnNavigationItemReselectedListener)
*/
@Deprecated
public void setOnNavigationItemSelectedListener(
@Nullable OnNavigationItemSelectedListener listener) {
setOnItemSelectedListener(listener);
Expand All @@ -205,14 +206,17 @@ public void setOnNavigationItemSelectedListener(
* @param listener The listener to notify
* @see #setOnNavigationItemSelectedListener(OnNavigationItemSelectedListener)
*/
@Deprecated
public void setOnNavigationItemReselectedListener(
@Nullable OnNavigationItemReselectedListener listener) {
setOnItemReselectedListener(listener);
}

/** Listener for handling selection events on bottom navigation items. */
@Deprecated
public interface OnNavigationItemSelectedListener extends OnItemSelectedListener {}

/** Listener for handling reselection events on bottom navigation items. */
@Deprecated
public interface OnNavigationItemReselectedListener extends OnItemReselectedListener {}
}
Expand Up @@ -312,7 +312,7 @@ public void setElevation(float elevation) {
* @param listener The listener to notify
* @see #setOnItemReselectedListener(OnItemReselectedListener)
*/
protected void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
selectedListener = listener;
}

Expand All @@ -323,7 +323,7 @@ protected void setOnItemSelectedListener(@Nullable OnItemSelectedListener listen
* @param listener The listener to notify
* @see #setOnItemSelectedListener(OnItemSelectedListener)
*/
protected void setOnItemReselectedListener(@Nullable OnItemReselectedListener listener) {
public void setOnItemReselectedListener(@Nullable OnItemReselectedListener listener) {
reselectedListener = listener;
}

Expand Down Expand Up @@ -681,7 +681,7 @@ public void removeBadge(int menuItemId) {
}

/** Listener for handling selection events on navigation items. */
protected interface OnItemSelectedListener {
public interface OnItemSelectedListener {

/**
* Called when an item in the navigation menu is selected.
Expand All @@ -695,7 +695,7 @@ protected interface OnItemSelectedListener {
}

/** Listener for handling reselection events on navigation items. */
protected interface OnItemReselectedListener {
public interface OnItemReselectedListener {

/**
* Called when the currently selected item in the navigation menu is selected again.
Expand Down
Expand Up @@ -260,37 +260,6 @@ protected NavigationRailMenuView createNavigationBarMenuView(@NonNull Context co
return new NavigationRailMenuView(context);
}

/**
* Sets a listener that will be notified when a navigation rail item is selected. This listener
* will also be notified when the currently selected item is reselected, unless an {@link
* OnNavigationItemReselectedListener} has also been set.
*
* @param listener The listener to notify
* @see #setOnNavigationItemReselectedListener(OnNavigationItemReselectedListener)
*/
public void setOnNavigationItemSelectedListener(
@Nullable OnNavigationItemSelectedListener listener) {
setOnItemSelectedListener(listener);
}

/**
* Sets a listener that will be notified when the currently selected navigation rail item is
* reselected. This does not require an {@link OnNavigationItemSelectedListener} to be set.
*
* @param listener The listener to notify
* @see #setOnNavigationItemSelectedListener(OnNavigationItemSelectedListener)
*/
public void setOnNavigationItemReselectedListener(
@Nullable OnNavigationItemReselectedListener listener) {
setOnItemReselectedListener(listener);
}

/** Listener for handling selection events on bottom navigation items. */
public interface OnNavigationItemSelectedListener extends OnItemSelectedListener {}

/** Listener for handling reselection events on bottom navigation items. */
public interface OnNavigationItemReselectedListener extends OnItemReselectedListener {}

private int makeMinWidthSpec(int measureSpec) {
int minWidth = getSuggestedMinimumWidth();
if (MeasureSpec.getMode(measureSpec) != MeasureSpec.EXACTLY && minWidth > 0) {
Expand Down

1 comment on commit 0ad4a8d

@anikethchavare
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

With the new Material updates, can you please tell me how to select a fragment?

Please sign in to comment.