无法在bottomNavigationView中显示徽章

问题描述 投票:0回答:1

我有一些方法可以在bottomNavigtionView的某些项目上显示徽章。这是:

private fun addBadgeView() {
        val menuView = mainNavigationView.getChildAt(0) as BottomNavigationMenuView
        val itemView = menuView.getChildAt(1) as BottomNavigationItemView
        val notificationBadge = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
        val textView = notificationBadge.findViewById<TextView>(R.id.counter_badge)
        val itemView1 = menuView.getChildAt(2) as BottomNavigationItemView
        val notificationBadgeOne = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
        val textView1 = notificationBadgeOne.findViewById<TextView>(R.id.counter_badge)

        val itemView2 = menuView.getChildAt(3) as BottomNavigationItemView
        val notificationBadge2 = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
        val textView2 = notificationBadge2.findViewById<TextView>(R.id.counter_badge)

        when {
            sp!!.getInt("notepad_count", 0) > 99 -> textView2.text = resources.getText(R.string.more90)
            sp!!.getInt("notepad_count", 0) < 99 -> textView2.text = sp!!.getInt("notepad_count", 0).toString()
            sp!!.getInt("notepad_count", 0) == 0 -> textView2.visibility = View.GONE
        }

        if (sp!!.getString("new_receivedM", "")!!.isNotEmpty()) {
            when {
                Integer.parseInt(sp!!.getString("new_receivedM", "")!!) > 99 -> textView.text = resources.getText(R.string.more90)
                sp!!.getString("new_receivedM", "") == "0" -> textView.visibility = View.GONE
                else -> textView.text = sp!!.getString("new_receivedM", "")
            }
        } else run { textView.visibility = View.GONE }


        if (sp!!.getString("all_jobs", "")!!.isNotEmpty()) {
            when {
                Integer.parseInt(sp!!.getString("all_jobs", "")!!) > 99 -> textView1.text = resources.getText(R.string.more90)
                sp!!.getString("all_jobs", "") == "0" -> textView1.visibility = View.GONE
                else -> textView1.text = sp!!.getString("all_jobs", "")
            }
        } else {
            textView1.visibility = View.GONE
        }

        itemView.addView(notificationBadge)
        itemView1.addView(notificationBadgeOne)
        itemView2.addView(notificationBadge2)
    }

但是有时候我想在某些情况下删除一些项目,因此我在这里添加了一些小条件:

val set = sp!!.getStringSet("disabled_app_modules", HashSet<String>())

if (set!!.isNotEmpty()) {
   val list: ArrayList<String> = ArrayList()
   list.addAll(set)
   for (i in 0 until list.size) {
    when (list[i].substring(1, list[i].length - 1)) {

      "notepad" -> {
         bottomNavigationView.menu.removeItem(R.id.notespec)
       }
     }
 }
}

并且我检查我的bottomNavigationView是否包含以下项目:

if (bottomNavigationView.getChildAt(2) != null) {
            val itemView1 = menuView.getChildAt(2) as BottomNavigationItemView
            val notificationBadgeOne = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
            val textView1 = notificationBadgeOne.findViewById<TextView>(R.id.counter_badge)

            if (sp!!.getString("all_jobs", "")!!.isNotEmpty()) {
                when {
                    Integer.parseInt(sp!!.getString("all_jobs", "")!!) > 99 -> textView1.text = resources.getText(R.string.more90)
                    sp!!.getString("all_jobs", "") == "0" -> textView1.visibility = View.GONE
                    else -> textView1.text = sp!!.getString("all_jobs", "")
                }
            } else {
                textView1.visibility = View.GONE
            }

            itemView1.addView(notificationBadgeOne)
        }

并且因此我看不到任何徽章,因为该项目是null,但是它是可见的并且可以单击。也许我在某处犯了一些错误?

更新

我知道我在哪里犯了错误:

if (bottomNavigationView.getChildAt(2) != null) 

此行将始终为空。因此,我删除了此条件,但出现了错误:

kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.material.bottomnavigation.BottomNavigationItemView

指向该行:

itemView2.addView(notificationBadge2)

据我了解,我打电话给已删除的项目。但是我不知道如何检查项目是否被删除。也许有人知道该怎么做?

android bottomnavigationview
1个回答
0
投票
implementation 'com.google.android.material:material:1.2.0-alpha03'

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

您的BottomNavigationView将是

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/bottom_nav_menu"
            />

活动中

val navBar = findViewById<BottomNavigationView>(R.id.bottom_navigation)
navBar.getOrCreateBadge(R.id.action_add).number = 2 //R.id.action_add is menu id
热门问题
推荐问题
最新问题