[英]Can't show badge at bottomNavigationView
我有一些方法可以在我的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)
據我所知,我打電話給已刪除的項目。 但我不知道如何檢查項目是否被刪除。 也許有人知道該怎么做?
讓我們輕松一點...:)
https://material.io/develop/android/components/badging/
implementation 'com.google.android.material:material:1.2.0-alpha03'
樣式文件
<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)
var badge = navBar.getOrCreateBadge(R.id.action_add) //R.id.action_add is menu id
badge.number = 2
badge.backgroundColor = //your color
badge.badgeTextColor = // your textcolor
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.