如何将片段目标绑定到底部导航栏中的菜单项?



我想在 android 中使用导航和目标系统,所以当单击底部导航栏中的菜单项时,它会转到一个片段。

我创建了 3 个空片段,并按照本指南将项目绑定到片段,但当我单击菜单项时,没有任何反应。 https://developer.android.com/guide/navigation/navigation-ui

我确保菜单中的项目 id 也与片段具有相同的 id。 我怎样才能做到这一点?

这是我的活动:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
bottom_navigation
.setupWithNavController(navController)
}  
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
}

这是活动 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:id="@+id/coordinator"
tools:context=".MainActivity">
<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/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemIconTint="@color/colorPrimaryDark"
app:itemTextColor="@color/colorPrimaryDark"
app:menu="@menu/bottom_navigation_menu" />

这是底部的导航栏菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fragment_home"
android:icon="@drawable/ic_home_24px"
android:title="Home" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search_24px"
android:title="Search" />    
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_face_24px"
android:title="Profile" />

导航.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.puntogris.herewego.home.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_profileFragment"
app:destination="@id/profileFragment" />
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.puntogris.herewego.profile.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
<action
android:id="@+id/action_profileFragment_to_searchFragment"
app:destination="@id/searchFragment" />
</fragment>
<fragment
android:id="@+id/searchFragment"
android:name="com.puntogris.herewego.search.SearchFragment"
android:label="fragment_search"
tools:layout="@layout/fragment_search" />

您的android:id不匹配。

菜单 XML 使用android:id="@+id/fragment_home",但导航 XML 使用android:id="@+id/homeFragment"

这些名称必须相同。例如,您可以将菜单 XML 更改为

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/homeFragment"
android:icon="@drawable/ic_home_24px"
android:title="Home" />
<item
android:id="@+id/searchFragment"
android:icon="@drawable/ic_search_24px"
android:title="Search" />    
<item
android:id="@+id/profileFragment"
android:icon="@drawable/ic_face_24px"
android:title="Profile" />
</menu>

最新更新