Android:从 Fragment 更新活动中底部导航视图的徽章计数

问题描述

我在我的应用程序中实现了带有徽章的底部导航视图,用于通知计数。我通过 DashBoardActivity 内的服务器调用设置计数,并且我有一个通知片段,我在其中显示所有通知。现在的问题是,当我点击任何通知时,我无法更新我的活动中的计数。我尝试使用界面更新计数,但似乎不起作用。

listener.onNotificationClick(count)

我也尝试创建一个变量并从片段更新它但没有效果

(activity as DashboardActivity).notificationsCount = count

DashBoardActivity.kt 我展示徽章的地方

class DashboardActivity : BaseActivity<>() {

private val viewmodel: DashBoardviewmodel by viewmodels()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_dashboard)

    viewmodel.fetchNotifications()

    observeNotifications()
}

private fun observeNotifications() {

    viewmodel.notificationLiveData.observe(this,Observer {
        when (it) {
            is NotificationResponse -> {
                showBadge(it.recordsTotal!!)
            }
            else -> {
                Log.e(it.message)
            }
        }
    })
}

private fun showBadge(count: Int) {
    val navBar: BottomNavigationView = mViewBinding.bottomNavigationView

    val badge = navBar.getorCreateBadge(R.id.nav_notifications_fragment)

    badge.backgroundColor = getColor(R.color.colorRed)
    badge.maxCharacterCount = 3
    badge.isVisible = count > 0
    badge.number = count
}

NotifcationFragment 我在那里听点击

class NotificationsFragment : BaseFragment<>(),BaseAdapter.BaseInterface {

override fun onNotificationClicked(dataType: Any?,view: View?,position: Int) {
    Update the counter
}

我做错了吗或者我应该怎么做才能解决这个问题

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)