ItemID 使用 ChipNavigationBar Android -> kotlin

ItemID using ChipNavigationBar Android -> kotlin

我在里面使用这个onCreate()。我为主页创建了四个片段,例如,搜索和个人资料。我从 https://github.com/ismaeldivita/chip-navigation-bar

得到这个 ChipNavgation
lateinit var btm_nav : ChipNavigationBar
    btm_nav = findViewById(R.id.btm_nav)
            val homeFragment = HomeFragment()
            val favoriteFragment = FavoriteFragment()
            val searchFragment = SearchFragment()
            val profileFragment = ProfileFragment()
    
            setCurrentFragment(homeFragment)
            btm_nav.setOnItemSelectedListener({
                when (it.itemId) {
                    R.id.home -> setCurrentFragment(homeFragment)
                    R.id.fav -> setCurrentFragment(favoriteFragment)
                    R.id.search -> setCurrentFragment(searchFragment)
                    R.id.profile -> setCurrentFragment(profileFragment)
    
                }
    
            })

但此代码不是 运行,无法访问 ItemID。 错误 -> 未解析的引用:itemId enter image description here

看图

setOnItemSelectedListener 方法回调本身返回菜单项的 idit 是菜单项的id。将 it.itemId 更改为 it

btm_nav.setOnItemSelectedListener({
            when (it) {
                R.id.home -> setCurrentFragment(homeFragment)
                R.id.fav -> setCurrentFragment(favoriteFragment)
                R.id.search -> setCurrentFragment(searchFragment)
                R.id.profile -> setCurrentFragment(profileFragment)

            }

})