How to dynamically hide a menu item in BottomNavig

2019-02-11 20:39发布

I want to hide a menu item of BottomNavigationView dynamically based on some conditions. I tried the following but it is not working.

mBottomNavigationView.getMenu()
            .findItem(R.id.item_name)
            .setVisible(false);

mBottomNavigationView.invalidate();

5条回答
smile是对你的礼貌
2楼-- · 2019-02-11 21:14

setVisibility should work for you. FYI, below example is in kotlin.

bottomNavigationView.menu.findItem(R.id.navigation_item_two).isVisible = true
查看更多
可以哭但决不认输i
3楼-- · 2019-02-11 21:17

In my case, I wanted to hide the toolbar text and the icons/titles of BottomNavigationView items in the authorization fragment, which handles the initial loading of my application. When it determines that the user is authenticated and fetches their profile from the database, I load the feed fragment, which fetches data from the database and displays it to the user. What I did was add the following method to the activity that creates the layout elements and call it from its fragments, passing in a boolean to determine visibility of the items.

public void setBottomNavigationViewItemsVisibility(boolean value) {
    if (this.bottomNavigationView != null) {
        this.bottomNavigationView.setVisibility(View.VISIBLE);
        Menu menu = this.bottomNavigationView.getMenu();
        if (value) {
            int[] icons = {R.drawable.ic_event_white_24dp, R.drawable.ic_explore,
                    R.drawable.ic_store_white_24dp, R.drawable.ic_notifications_white_24dp};
            int[] titles = {R.string.feed, R.string.explore, R.string.finder, R.string.notifications};
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setIcon(icons[i]);
                menu.getItem(i).setTitle(titles[i]);
                menu.getItem(i).setEnabled(true);
            }
        } else {
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setIcon(R.drawable.ic_empty);
                menu.getItem(i).setTitle(R.string.title_empty);
                menu.getItem(i).setEnabled(false);
            }
        }
    }
}

We declare an array of drawable ids and an array of title ids to match what we have declared in the menu XML file. If true, we iterate through the menu items and set their icon, title, and their state to default values. If false, we set the icon to a transparent icon (removing the icon affects its size), set the toolbar title to an empty string, and disable it.

BottomNavigationView Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_navigation_view_home">

    <item
        android:id="@+id/action_feed"
        android:enabled="true"
        android:icon="@drawable/ic_event_white_24dp"
        android:title="@string/feed"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_explore"
        android:enabled="true"
        android:icon="@drawable/ic_explore"
        android:title="@string/explore"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_finder"
        android:enabled="true"
        android:icon="@drawable/ic_store_white_24dp"
        android:title="@string/finder"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_notifications"
        android:enabled="true"
        android:icon="@drawable/ic_notifications_white_24dp"
        android:title="@string/notifications"
        app:showAsAction="ifRoom" />
</menu>

Empty Icon (ic_empty.xml):

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#00FFFFFF"
        android:pathData="M8" />
</vector>

Empty Title (title_empty):

<string name="title_empty" />
查看更多
做个烂人
4楼-- · 2019-02-11 21:18

I tried most of solutions but this worked for me,

For hiding an item dynamically : bottomNavigationView.findViewById(R.id.xyz).setVisibility(View.GONE);

For making item visible: bottomNavigationView.findViewById(R.id.xyz).setVisibility(View.VISIBLE);

查看更多
▲ chillily
5楼-- · 2019-02-11 21:29

The removeItem displaces the menu items in the bar when an item(s) is hidden. I found a slightly better way. Create a group of menu items that you would want to hide your menu xml.

In your bottom_menu.xml

<menu...>
    <group android:id="@+id/hiddenmenu">
        <item.../>
        <item.../>
    </group>
    <item.../>
</menu>

And in your activity.cs

Menu menu = mbottomNavigation.getMenu();  
menu.getMenu.setGroupEnabled(R.id.hiddenmenu, false);

Although, with this setup, when all menu items are visible, the checked change state of the menu items goes out of whack. Also tried programmatically adding menu items to an empty group but the group stopped responding to the GroupDisable...

查看更多
beautiful°
6楼-- · 2019-02-11 21:34
mBottomNavigationView.getMenu().removeItem(R.id.item_name);

removeItem does the trick. Not sure why setVisible method is not working.

查看更多
登录 后发表回答