在底部导航视图中不设置预选项目

Set no item pre-selected in Bottom Navigation view

我正在将 material 设计库中的新底部导航视图添加到项目中,我希望默认情况下没有预选项目。

目前默认选中第一项。

我用过

mBottomNavigation.getMenu().getItem(0).setChecked(false);

但是在 for 循环中为所有菜单项执行此操作时,默认情况下会再次选择最后一项。

我们有什么办法可以做到这一点吗?

不确定实现此目的的正确方法,但解决方法会有所帮助-

  1. setCheckable(false) 第一项

    navigation.getMenu().getItem(0).setCheckable(false);

  2. item.setCheckable(true) inside onNavigationItemSelected()

    public boolean onNavigationItemSelected(MenuItem item) {
    
    switch (item.getItemId()) {
        case R.id.navigation_home:
            item.setCheckable(true);
            mTextMessage.setText(R.string.title_home);
            return true;
      }
      return false;
    }
    

在你的 onCreate 方法中添加这一行

mBottomNavigation.setSelectedItemId("ID OF YOUR MENU ITEM");

我创建了一个示例项目,默认情况下没有选中的项目。只需确保您的代码中没有任何 navigationView.setCheckedItem(R.id.id)

我结合了@Ashish Kumar 提到的解决方案并解决了我的查询

private void customizeBottomBar() {
        mBottomNavigation.getMenu().getItem(0)
                .setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_reserve_normal));
        changeMenuItemCheckedStateColor(mBottomNavigation, getUnCheckedColor(), getUnCheckedColor());
    }

/**
   * Method to change the color state of bottom bar view
   * @param bottomNavigationView - BottomNavigation view instance
   * @param checkedColorHex int value of checked color code
   * @param uncheckedColorHex int value of unchecked color code
   */
  void changeMenuItemCheckedStateColor(BottomNavigationView bottomNavigationView,
      int checkedColorHex, int uncheckedColorHex) {

    int[][] states = new int[][]{
        new int[]{-android.R.attr.state_checked}, // unchecked
        new int[]{android.R.attr.state_checked}, // checked
    };

    int[] colors = new int[]{
        uncheckedColorHex,
        checkedColorHex
    };

    ColorStateList colorStateList = new ColorStateList(states, colors);
    bottomNavigationView.setItemTextColor(colorStateList);
    bottomNavigationView.setItemIconTintList(colorStateList);

  }

在行中使用 setCheckable 而不是 setChecked(false)

mBottomNavigation.getMenu().getItem(0).setCheckable(false);

对我有用。

XML代码

<group android:checkableBehavior="single">
    <item              android:id="@+id/nav_item1" />
    <item              android:id="@+id/nav_item2" />
    <item              android:id="@+id/nav_item3" />
    <item              android:id="@+id/nav_item4" />
    <item
        android:id="@+id/nav_item5"
        android:icon="@drawable/icon_item5"
        android:title="Home"
        android:visible="false"/>
</group>

JAVA代码

bottomNavigationView.getMenu().getItem(4).setChecked(true);

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            switch (menuItem.getItemId()) {
                case R.id.nav_item1:
                    return true;

                case R.id.nav_item2:
                    return true;

                case R.id.nav_item3:
                    return true;

                case R.id.nav_item4:
                    return true;
            }

            // Default operation you want to perform

            return false;
        }
    });

如果有人对好的 Kotlin 解决方案感兴趣,这是我的:

//disable the preselected first item
//<navigation> is the bottom navigation view

navigation.menu.getItem(0).isCheckable=false

然后在选择监听器中,确保向用户显示他选择的内容

BottomNavigationView.OnNavigationItemSelectedListener { item: MenuItem ->
        when (item.itemId) {

            R.id.option1 -> {
                item.isCheckable=true //here is the magic

                //notify the listener
                return@OnNavigationItemSelectedListener true
            }
            R.id.option2 ->{
               item.isCheckable=true

                //notify the listener
                return@OnNavigationItemSelectedListener true
            }
            R.id.option3 ->{
                //go to forgot user fragment
                item.isCheckable=true

                //notify the listener
                return@OnNavigationItemSelectedListener true
            }

            else -> false
        }


    }

最后,制作一个选择器颜色,以便您可以在 color

中轻松更改
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
      android:color="@color/colorAccent" />
<item android:color="@color/colorPrimary"  />

并将选择器添加到导航视图

 app:itemIconTint="@color/navigation_colors"
 app:itemTextColor="@color/navigation_colors"

现在,如果您需要更改颜色,只需更改选择器即可。

我想到了另一个解决方案

例如,只需在 menu.xml 文件中再添加一项

这是我的bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/home" />

    <item
        android:id="@+id/navigation_cart"
        android:icon="@drawable/ic_shopping_cart_black_24dp"
        android:title="@string/cart" />

    <item
        android:id="@+id/navigation_wishlist"
        android:icon="@drawable/ic_favorite_border_black_24dp"
        android:title="@string/wish_list" />

    <item
        android:id="@+id/navigation_account"
        android:icon="@drawable/ic_person_black_24dp"
        android:title="@string/account" />

    
    <!-- Our invisible item -->

    <item
        android:id="@+id/invisible"
        android:visible="false"
        android:icon="@drawable/ic_person_black_24dp"
        android:title="@string/account" />

</menu>

请注意,我已在最后位置添加了该项目并为其指定了一个 ID invisible 并将其可见性设置为 false。

现在,在 activity 中,只需将所选项目 ID 设置为此 ID,就像这样

bottomNavMenu.setSelectedItemId(R.id.invisible);

谢谢