底部导航栏监听器不工作

Bottom navigation bar listener not working

所以我几周前才开始 android 开发,当时我试图在带有底部导航栏的两个片段之间进行导航,但由于某些原因,点击监听器无法正常工作,没有错误,没有警告,编译没问题,但可能是一些逻辑问题?这是代码:

class MainActivity : AppCompatActivity() {

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

        val fragmentHome = HomeFragment()
        val fragmentProfile = ProfileFragment()

        replaceCurrentFragment(fragmentHome)

        NavigationBarView.OnItemSelectedListener { item ->
            when(item.itemId) {
                R.id.page_home -> {
                    Log.i("NavBar","Home pressed")
                    replaceCurrentFragment(fragmentHome)
                    true
                }
                R.id.page_profile -> {
                    Log.i("NavBar","Profile pressed")
                    replaceCurrentFragment(fragmentProfile)
                    true
                }
                else -> {
                    Log.i("NavBar","Error?")
                    false
                }
            }
        }
    }

    private fun replaceCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.flFragment, fragment)
            commit()
        }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/flFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        app:labelVisibilityMode="selected"
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/page_home"
        android:icon="@drawable/ic_home"
        android:title="Home"/>
    <item
        android:id="@+id/page_profile"
        android:icon="@drawable/ic_profile"
        android:title="Profile"/>
</menu>

主页确实在主页片段上进行了初始化,但是当我单击导航栏按钮时,没有任何反应所以问题出在该侦听器部分,从文档复制粘贴但它不起作用。

我还有一个错误,布局编辑器不显示导航栏内容,所以我的 ide 可能出了问题?将不胜感激。

将“NavigationBarView”替换为您实际的 bottomNavigationView id。

这行得通,

class MainActivity : AppCompatActivity() {

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

        val fragmentHome = HomeFragment()
        val fragmentProfile = ProfileFragment()
        replaceCurrentFragment(fragmentHome)

val myBottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation)

myBottomNavigationView.OnItemSelectedListener { item ->
            when(item.itemId) {
                R.id.page_home -> {
                    Log.i("NavBar","Home pressed")
                    replaceCurrentFragment(fragmentHome)
                    true
                }
                R.id.page_profile -> {
                    Log.i("NavBar","Profile pressed")
                    replaceCurrentFragment(fragmentProfile)
                    true
                }
                else -> {
                    Log.i("NavBar","Error?")
                    false
                }
            }
        }
    }

    private fun replaceCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.flFragment, fragment)
            commit()
        }
}