【问题标题】:Removal of Badge from BottomNavigationView when using navigation component使用导航组件时从 BottomNavigationView 中移除 Badge
【发布时间】:2020-12-24 23:33:51
【问题描述】:

我正在使用材料设计 BottomNavigationView 组件和使用导航组件的片段过渡。

我目前面临的问题是我无法移除/隐藏徽章。当我们使用 navigationComponent 我不能使用 setOnNavigationItemSelectedListener 而不是我们使用 setupWithNavController 所以当我点击 itemId 时,我如何知道我当前在哪个片段/itemId 上并删除该特定 itemId 的徽章

【问题讨论】:

    标签: android kotlin android-navigation badge android-bottomnavigationview


    【解决方案1】:

    使用自定义 NavigationItemSelectedListener 您正在删除原始侦听器。但是,您可以使用 NavigationUI.onNavDestinationSelected(it, navController) 触发默认行为。

    bottomNavigationView.setOnNavigationItemSelectedListener {
                when (it.itemId) {
                    R.id.nav_xxxx -> {
                        // handle click
                        // .....
                    }
                }
                NavigationUI.onNavDestinationSelected(it, navController)
                true
            }
    

    【讨论】:

      【解决方案2】:

      创建一个选择器 XML 以更改所选菜单项的图标。假设它叫bottom_item_selector

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:drawable="@drawable/unchecked_item_drawable" android:state_checked="false"/>
          <item android:drawable="@drawable/checked_item_drawable" android:state_checked="true"/>
      </selector>
      

      然后在您的菜单 XML 中更改图标以使用此可绘制对象:

          <item
              android:id="@+id/.."
              android:icon="@drawable/bottom_item_selector"
              android:checkable="true"
              android:title=""/>
      

      这样您就不必添加侦听器并显式调用NavigationUI

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多