Android Jetpack Navigation navController 问题
Android Jetpack Navigation navController problems
无法理解为什么它不起作用...
另一个模拟项目 bnv 工作正常...
应用内 app:startDestination="@+id/navigate_hot"
工作正常,但点击 bnv
项目 - 没有任何反应
mainActivity.XML
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bttm_nav"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:elevation="4dp"
app:itemIconSize="35dp"
app:itemTextColor="@color/bnv_tab_item_foreground"
app:itemIconTint="@color/bnv_tab_item_foreground"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_nav_menu"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/bnv_navigation" />
MainActivity.kt
override fun onSupportNavigateUp(): Boolean {
return findNavController(this@ActivityMain,R. id.nav_host_fragment).navigateUp()
}
private fun setupNavigation() {
val navController = findNavController(this@ActivityMain, R.id.nav_host_fragment)
bttm_nav.setupWithNavController(navController)
}
bnv_navigation.XML
<navigation 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:id="@+id/nav_graph"
app:startDestination="@+id/navigate_hot">
<fragment
android:id="@+id/navigate_news"
android:name="com.example.fatsale.fragments.NewsFragment"
android:label="News"
tools:layout="@layout/fragment_news" />
<fragment
android:id="@+id/navigate_hot"
android:name="com.example.fatsale.fragments.HotFragment"
android:label="Collection"
tools:layout="@layout/fragment_hot" />
<fragment
android:id="@+id/navigate_profile"
android:name="com.example.fatsale.fragments.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile" />
</navigation>
告诉这个笨蛋需要做什么..
在 bottomnavigationview 的菜单文件中,每个项目 ID 必须与添加到导航图 xml 文件中的片段 ID 相同。同样在菜单文件中,每个项目都必须为 bottomnavigationview 中的片段位置 no 提供 OrderInCategory。
无法理解为什么它不起作用...
另一个模拟项目 bnv 工作正常...
应用内 app:startDestination="@+id/navigate_hot"
工作正常,但点击 bnv
项目 - 没有任何反应
mainActivity.XML
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bttm_nav"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:elevation="4dp"
app:itemIconSize="35dp"
app:itemTextColor="@color/bnv_tab_item_foreground"
app:itemIconTint="@color/bnv_tab_item_foreground"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_nav_menu"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/bnv_navigation" />
MainActivity.kt
override fun onSupportNavigateUp(): Boolean {
return findNavController(this@ActivityMain,R. id.nav_host_fragment).navigateUp()
}
private fun setupNavigation() {
val navController = findNavController(this@ActivityMain, R.id.nav_host_fragment)
bttm_nav.setupWithNavController(navController)
}
bnv_navigation.XML
<navigation 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:id="@+id/nav_graph"
app:startDestination="@+id/navigate_hot">
<fragment
android:id="@+id/navigate_news"
android:name="com.example.fatsale.fragments.NewsFragment"
android:label="News"
tools:layout="@layout/fragment_news" />
<fragment
android:id="@+id/navigate_hot"
android:name="com.example.fatsale.fragments.HotFragment"
android:label="Collection"
tools:layout="@layout/fragment_hot" />
<fragment
android:id="@+id/navigate_profile"
android:name="com.example.fatsale.fragments.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile" />
</navigation>
告诉这个笨蛋需要做什么..
在 bottomnavigationview 的菜单文件中,每个项目 ID 必须与添加到导航图 xml 文件中的片段 ID 相同。同样在菜单文件中,每个项目都必须为 bottomnavigationview 中的片段位置 no 提供 OrderInCategory。