【问题标题】:Remove badge from BottomNavigation从底部导航中删除徽章
【发布时间】: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 的可见性。查看他的答案,他删除了先前实例化的视图!

标签: android android-bottomnav


【解决方案1】:

找到了解决办法。

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

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)
    }
}

【讨论】:

    【解决方案2】:

    在我的例子中,我在 badgeView 中添加了TAG,并通过 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      相关资源
      最近更新 更多