我根据以下主题实现了一个计数器徽章。
然后我稍微扩展了一下,当通知计数为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时,徽章没有被移除,我似乎找不到原因。
bottomNavigationItemView.addView(inboxBadge);
做了相反的操作...而且如果计数为0,膨胀视图有什么意义呢? - undefinednotificationCount.setVisibility(GONE);
中,他设置了一个新实例化的notificationCount
的可见性。看看他的回答,他删除了之前实例化的视图! - undefined