【发布时间】:2018-07-04 16:24:39
【问题描述】:
我根据following thread 实现了一个计数器徽章。
然后我花了一点时间在通知计数为 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);相反的事情......如果count为0,那么膨胀视图的意义何在? -
事后编辑:现在你正在删除新膨胀的项目......显然在 bottomNavigationItemView 中不存在......所以显然“什么也没发生”......
-
明显的解决方案:1.只创建一次inboxBadge 2.存储对它的引用3.利用setVisibility
-
@Selvin 我也尝试过全局声明徽章(应该指向同一个实例)并在删除时引用它。也没用。
-
他确实有问题。为什么要投票?在
notificationCount.setVisibility(GONE);行中,他正在设置新实例化notificationCount的可见性。查看他的答案,他删除了先前实例化的视图!