【问题标题】:Latest BottomNavigtionView [duplicate]最新的 BottomNavigationView [重复]
【发布时间】:2021-08-31 10:07:59
【问题描述】:

我正在创建一个 Android 应用程序,我偶然发现以前创建底部导航视图的方法已被弃用。

这是我得到的错误:

'setOnNavigationItemReselectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemReselectedListener)' is deprecated

我到处搜索,但没有网站显示最新和更新的方法。

创建正确的 BottomNavigtionView 的最新方法是什么?提前致谢

【问题讨论】:

标签: java android deprecated bottomnavigationview android-bottomnavigationview


【解决方案1】:

是的 setOnNavigationItemReselectedListener 现在已被弃用。要在 Google 底部导航上执行点击操作,您必须在您的活动/片段中使用 bottomNavigation.setOnItemSelectedListener(this) 并扩展 NavigationBarView.OnItemSelectedListener

bottomNavigation.setOnItemSelectedListener(this) 然后覆盖onNavigationItemSelected(item: MenuItem)

例如Kotlin:

bottomNavigation.setOnItemSelectedListener(this)
override fun onNavigationItemSelected(item: MenuItem): Boolean {

    when(item.itemId){
        R.id.your_menu_id->{
            Toast.makeText(this,"Click",Toast.LENGTH_SHORT).show()
        }
        ///
    }
    return true
}

bottomNavigationView.setOnItemSelectedListener{
        when (it.itemId) {
            R.id.your_menu_id-> {
               Toast.makeText(this,"Click",Toast.LENGTH_SHORT).show()
                return@setOnItemSelectedListener true
            }
           ///
        }
        false
    }

对于重选,使用 bottomNav.setOnItemReselectedListener(this) 并扩展 NavigationBarView.OnItemReselectedListener 并覆盖 onNavigationItemReselected(item: MenuItem)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多