我是Kotlin开发的新手。我试图做一个BottomNavigationView,一切正常,但是当我想为ItemSelected侦听器创建BottomNavigationView变量时,出现以下错误:
Function invocation 'BottomNavigationView(...)' expected.
但是在我的教程中,它的声明如下:
BottomNavigationView bottomNav
所以怎么了,我不明白..
你能帮我吗 ?
最佳答案
为bottomViewListener声明一个全局变量
'onNavigationItemSelectedListener'
在onCreate()的bottomView中初始化和设置侦听器
val navView:BottomNavigationView = findViewById(R.id.nav_view)
navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
return@OnNavigationItemSelectedListener true
}
R.id.navigation_deals -> {
return@OnNavigationItemSelectedListener true
}
}
false
}