gpt4 book ai didi

android - 从 BottomNavigation 中移除徽章

转载 作者:行者123 更新时间:2023-11-29 15:36:35 24 4
gpt4 key购买 nike

我已经根据 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 时徽章没有被删除,我似乎无法找出原因。

最佳答案

找到解决方案。

我在菜单项中找到实际徽章并将其移除,然后最终生成一个新徽章。这是唯一适合我的方法:

fun setInboxIcon(count: Int) {
val bottomNavigationMenuView = bottomNavigation.getChildAt(0) as BottomNavigationMenuView
val bottomNavigationItemView = bottomNavigationMenuView.getChildAt(3) as BottomNavigationItemView
val badge = LayoutInflater.from(context).inflate(R.layout.inbox_icon_layout, bottomNavigationMenuView, false)
val notificationCount = badge.findViewById(R.id.notification_count)

// Reset current badge
bottomNavigationItemView.removeView(bottomNavigationItemView.getChildAt(2))

// Add new badge
if (count > 0) {
notificationCount.text = Math.min(count, 9).toString()
bottomNavigationItemView.addView(badge)
}
}

关于android - 从 BottomNavigation 中移除徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48442563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com