Android 从底部导航中移除徽章
我已经实施了一个柜台徽章根据 然后,当通知计数为0时,我花费了一点时间从导航项中删除徽章:Android 从底部导航中移除徽章,android,android-bottomnav,Android,Android Bottomnav,我已经实施了一个柜台徽章根据 然后,当通知计数为0时,我花费了一点时间从导航项中删除徽章: fun setInboxIcon(count: Int) { val bottomNavigationMenuView = bottomNavigation.getChildAt(0) as BottomNavigationMenuView val bottomNavigationItemView = bottomNavigationMenuView.getChildAt(3) as Bo
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()
}
fun setinboxion(计数:Int){
val bottomNavigationMenuView=bottomNavigation.getChildAt(0)作为bottomNavigationMenuView
val bottomNavigationItemView=bottomNavigationMenuView.getChildAt(3)作为bottomNavigationItemView
val inboxBadge=LayoutInflater.from(上下文)。充气(R.layout.inbox_图标_布局,底部导航菜单视图,false)
notificationCount=inboxBadge.findViewById(R.id.notification\U计数)
如果(计数=0){
notificationCount.visibility=GONE
notificationCount.text=“”
bottomNavigationItemView.removeView(inboxBadge)//找到了解决方案
我在菜单项中找到了实际的徽章并将其移除,然后最终生成一个新徽章。这是唯一适合我的方法:
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)
}
}
找到了解决办法
我在菜单项中找到了实际的徽章并将其移除,然后最终生成一个新徽章。这是唯一适合我的方法:
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)
}
}
在我的例子中,我在badgeView中添加了TAG
,并按标记查找视图以删除它
private val TAG = "Badge"
fun addOrRemoveBadgeView(bottomNav: BottomNavigationView, show: Boolean) {
val menuView = bottomNav.getChildAt(0) as BottomNavigationMenuView
val itemView = menuView.getChildAt(3) as BottomNavigationItemView
val notificationsBadge = LayoutInflater.from(bottomNav.context)
.inflate(R.layout.badge_layout,menuView, false)
notificationsBadge.tag = TAG
if (show) {
itemView.addView(notificationsBadge)
}
else {
val view = itemView.findViewWithTag<View>(TAG)
itemView.removeView(view)
}
}
private val TAG=“徽章”
fun addOrRemoveBadgeView(底部导航:底部导航视图,显示:布尔值){
val menuView=bottomNav.getChildAt(0)作为BottomNavigationMenuView
val itemView=menuView.getChildAt(3)作为BottomNavigationItemView
val notificationsBadge=LayoutInflater.from(bottomNav.context)
.充气(右布局、徽标_布局、菜单视图、假)
notificationsBadge.tag=标签
如果(显示){
itemView.addView(notificationsBadge)
}
否则{
val view=itemView.findViewWithTag(标记)
itemView.removeView(视图)
}
}
在我的例子中,我在badgeView中添加了标记
,并按标记查找视图以删除它
private val TAG = "Badge"
fun addOrRemoveBadgeView(bottomNav: BottomNavigationView, show: Boolean) {
val menuView = bottomNav.getChildAt(0) as BottomNavigationMenuView
val itemView = menuView.getChildAt(3) as BottomNavigationItemView
val notificationsBadge = LayoutInflater.from(bottomNav.context)
.inflate(R.layout.badge_layout,menuView, false)
notificationsBadge.tag = TAG
if (show) {
itemView.addView(notificationsBadge)
}
else {
val view = itemView.findViewWithTag<View>(TAG)
itemView.removeView(view)
}
}
private val TAG=“徽章”
fun addOrRemoveBadgeView(底部导航:底部导航视图,显示:布尔值){
val menuView=bottomNav.getChildAt(0)作为BottomNavigationMenuView
val itemView=menuView.getChildAt(3)作为BottomNavigationItemView
val notificationsBadge=LayoutInflater.from(bottomNav.context)
.充气(右布局、徽标_布局、菜单视图、假)
notificationsBadge.tag=标签
如果(显示){
itemView.addView(notificationsBadge)
}
否则{
val view=itemView.findViewWithTag(标记)
itemView.removeView(视图)
}
}
问题出在哪里?显然,您必须执行与底部导航项目视图.addView(inboxBadge);
相反的操作。…如果计数为0,则膨胀视图的目的是什么?R编辑后:现在您正在移除新膨胀的项目…底部导航项目视图中显然不存在此项目…因此“什么都没有发生”…显而易见的解决方案:1.只创建一次inboxBadge 2.存储对它的引用3.利用setVisibility@Selvin我还尝试过全局声明徽章(应该指向同一个实例)并且在移除时引用它。也没有帮助。他确实有问题。为什么要进行否决?在notificationCount.setVisibility(消失)行中;
他正在设置新实例化的通知计数的可见性。在他删除以前实例化的视图的地方查看他的答案!问题出在哪里?显然你做了与底部导航项视图.addView(inboxBadge)相反的事
…如果计数为0,则膨胀视图的目的是什么?编辑后:现在您正在删除新膨胀的项目…它显然不存在于bottomNavigationItemView中…因此“什么都没有发生”…显而易见的解决方案:1.只创建一次inboxBadge 2.存储对它的引用3.利用setVisibility@Selvin我还尝试过全局声明徽章(应该指向同一个实例)并且在删除时引用它。也没有帮助。他确实有问题。为什么要进行向下投票?在notificationCount.setVisibility(GONE);
行中,他正在设置一个新实例化的notificationCount
的可见性。在他删除以前实例化的视图的地方查看他的答案!