底部导航栏侦听器不工作



所以我几周前刚刚开始开发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"与您的实际底部导航查看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()
}
}

最新更新