【问题标题】:Set no item pre-selected in Bottom Navigation view在底部导航视图中不设置预选项目
【发布时间】:2017-06-12 11:39:45
【问题描述】:

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

现在默认选择第一个项目。

我用过

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

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

有什么方法可以实现吗?

【问题讨论】:

  • 请分享您的代码。
  • 我只是在初始化视图并在循环中使用上面的代码
  • for (int i = 0; i

标签: android bottomnavigationview


【解决方案1】:

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

  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;
    }
    

【讨论】:

  • 你工作正常..非常感谢。为你投票。
  • @Ashish Kumar 我想知道为什么 setChecked 不适用于 26 岁及以上必须使用 setCheckable
  • 有效,但仅使用图标时,文本仍然显示,如何从下面删除文本?
  • @JaySmoke app:labelVisibilityMode="unlabeled"
【解决方案2】:

我想出了另一个解决方案

例如,只需在 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。

现在,在活动中,只需像这样将所选项目 id 设置为这个 id

bottomNavMenu.setSelectedItemId(R.id.invisible);

谢谢

【讨论】:

  • 这是我最喜欢的答案。在框架具有无选择功能之前,不要使用框架句柄。竖起大拇指
  • 效果很好!为了简洁一点,我们可以使用 id 和 visible 属性。
  • 如果您已经有 5 个选项卡,则无法使用,因为 BottomNavigationView 允许的最大选项卡为 5 个。会抛出一个
【解决方案3】:

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;
        }
    });

【讨论】:

    【解决方案4】:

    如果有人对一个好的 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"
    

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

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

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

        它对我有用。

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              猜你喜欢
              • 2017-03-07
              • 1970-01-01
              • 2017-04-01
              • 1970-01-01
              • 2017-06-09
              • 1970-01-01
              • 2017-03-12
              • 2018-03-06
              • 2017-09-15
              相关资源
              最近更新 更多