我正在使用带有 Kotlin 的 Android Jetpack 使用 NavigationHostFragment、Navgraph 和 NavController 创建这个 BottomBarNavigation UI。
我有 3 个选项卡,一切都按预期工作,除非我尝试单击第一个选项卡,但它什么也没做。
卡了几天。找不到问题。有任何想法吗?
bottom_tabbar_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_tabbar_nav_graph"
app:startDestination="@id/transactionFragment">
<fragment
android:id="@+id/transactionFragment"
android:name="com.example.project_budget_planner.main.transaction.TransactionFragment"
android:label="fragment_transaction" />
<fragment
android:id="@+id/statisticsFragment"
android:name="com.example.project_budget_planner.main.statistics.StatisticsFragment"
android:label="fragment_statistics" />
<fragment
android:id="@+id/settingsFragment"
android:name="com.example.project_budget_planner.main.settings.SettingsFragment"
android:label="fragment_settings" />
</navigation>
bottom_tabbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/trasactionFragment"
android:icon="@drawable/common_google_signin_btn_icon_dark"
android:orderInCategory="1"
app:showAsAction="always"
android:title="Transaction" />
<item
android:id="@+id/statisticsFragment"
android:icon="@drawable/common_google_signin_btn_icon_dark"
android:orderInCategory="2"
app:showAsAction="always"
android:title="Statistics" />
<item
android:id="@+id/settingsFragment"
android:icon="@drawable/common_google_signin_btn_icon_dark"
android:orderInCategory="3"
app:showAsAction="always"
android:title="Settings" />
</menu>
activity_main.xml
<?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=".main.MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/bottom_navbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/bottom_tabbar_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ccc"
app:itemIconTint="#472B78"
app:itemTextColor="#472C67"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_tabbar_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
private fun setupNavigation() {
val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottom_navbar)
val navHostFragment: NavHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)
}