我已经根据以下线程实现了一个计数器徽章。
然后,当通知计数为 0 时,我花了一点时间从导航项中删除徽章:
fun setInboxIcon(count: Int) {
val bottomNavigationMenuView = bottomNavigation.getChildAt(0) as BottomNavigationMenuView
val bottomNavigationItemView = bottomNavigationMenuView.getChildAt(3) as BottomNavigationItemView
val inboxBadge = LayoutInflater.from(context).inflate(R.layout.inbox_icon_layout, bottomNavigationMenuView, false)
notificationCount = inboxBadge.findViewById(R.id.notification_count)
if (count == 0) {
notificationCount.visibility = GONE
notificationCount.text = ""
bottomNavigationItemView.removeView(inboxBadge) // <- nothing happens
} else {
notificationCount.visibility = VISIBLE
notificationCount.text = Math.min(count, 9).toString()
bottomNavigationItemView.addView(inboxBadge)
}
bottomNavigation.invalidate()
}
问题是当通知计数为 0 时徽章没有被移除,我似乎无法找出原因。