Kev*_*inM 6 android android-navigation android-drawer android-jetpack android-jetpack-navigation
我很好奇如何使用 Android Jetpack 中的导航图从抽屉布局中的菜单项导航到嵌套导航图。我知道幕后有一些魔法可以将菜单项与基于 Id 的片段链接起来,但我不知道如何将菜单项链接到嵌套导航图。
例如,我使用 Android Studio 附带的默认导航抽屉活动项目。我已将 mobile_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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.example.testdrawer.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<include app:graph="@navigation/nested_navigation" />
<fragment
android:id="@+id/nav_tools"
android:name="com.example.testdrawer.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />
<fragment
android:id="@+id/nav_share"
android:name="com.example.testdrawer.ui.share.ShareFragment"
android:label="@string/menu_share"
tools:layout="@layout/fragment_share" />
<fragment
android:id="@+id/nav_send"
android:name="com.example.testdrawer.ui.send.SendFragment"
android:label="@string/menu_send"
tools:layout="@layout/fragment_send" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
我还添加了一个名为nested_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/nested_navigation"
app:startDestination="@+id/nav_gallery">
<fragment
android:id="@+id/nav_gallery"
android:name="com.example.testdrawer.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/nav_slideshow"
android:name="com.example.testdrawer.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
假设我的菜单如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/nested_navigation"
android:icon="@drawable/ic_menu_gallery"
android:title="Photos Graph" />
<item
android:id="@+id/nav_tools"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="@string/menu_send" />
</menu>
</item>
</menu>
Run Code Online (Sandbox Code Playgroud)
所以本质上,我只想知道如何点击抽屉中的菜单项导航到嵌套导航图的起始目的地。
编辑:我已经想出了如何通过实现 NavigationView.OnNavigationItemSelectedListener 接口并通过全局操作手动触发导航来做到这一点:
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.nested_navigation -> {
findNavController(R.id.nav_host_fragment).navigate(R.id.action_nested_navigation)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
Run Code Online (Sandbox Code Playgroud)
我仍然希望有一种方法可以通过自动命名约定来做到这一点。
任何帮助将不胜感激,非常感谢!
小智 0
事实上,我以一种非常标准的方式工作。您的 XML 文件看起来没问题,但您是否也将 R.id.nested_navigation 包含到 AppBarConfiguration 中的 topLevelDestinationIds 中?
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home,
R.id.nav_tools,
R.id.nav_share,
R.id.nav_send.
R.id.nested_navigation
),
drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
Run Code Online (Sandbox Code Playgroud)
如果这不能解决您的问题,请分享您设置导航视图的代码。
| 归档时间: |
|
| 查看次数: |
714 次 |
| 最近记录: |