在底部导航视图中不预先选择任何项目

MRX*_*MRX 2 android bottomnavigationview

我正在将新的底部导航视图从材料设计库添加到项目中,并且我希望默认情况下没有预先选择的项目.

目前,默认选择第一项.

我用过

mBottomNavigation.getMenu().getItem(0).setChecked(false);
Run Code Online (Sandbox Code Playgroud)

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

有没有办法实现这个目标?

Age*_*ntP 6

我想出了另一个解决方案

例如,只需在 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>
Run Code Online (Sandbox Code Playgroud)

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

现在,在活动中,只需像这样将选定的项目 ID 设置为此 ID

bottomNavMenu.setSelectedItemId(R.id.invisible);
Run Code Online (Sandbox Code Playgroud)

谢谢

  • 这是我喜欢的最佳答案。在框架没有任何选择的功能之前,不要玩弄框架句柄。竖起大拇指 (2认同)

Ash*_*mar 5

不确定实现这一目标的正确方法,但解决方案将有助于 -

  1. 第一项的setCheckable(false)

    .navigation.getMenu()的getItem(0).setCheckable(假);

  2. onNavigationItemSelected()中的item.setCheckable(true)

    public boolean onNavigationItemSelected(MenuItem item){

    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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }

  • @Ashish Kumar 我想知道为什么 setChecked 不适用于 26 及以上必须使用 setCheckable (2认同)

MRX*_*MRX -1

我结合了 @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);

  }
Run Code Online (Sandbox Code Playgroud)