如何在导航架构中导航抽屉标题操作的任何目的地 [英] How to Navigate any destination for action of header of drawer in navigation architecture
问题描述
请大家解释一下,如何在导航架构中为抽屉的标题布局定义动作.
Anyone please explain, How to define the action in Navigation architecture for header layout of drawer.
现在,我需要设置抽屉标题的点击,我把它设置成这样:
Now, I need to set click of header of drawer and I set it to like this:
headerOfNavDrawer.setOnClickListener{
//Here I want to navigate to editProfileFragment
//But For navigation I need an action in nav arch graph.
//Where to put action??
}
推荐答案
您需要做两件事:
- 对
NavController
的引用.
根据导航到目标文档,您可以使用findNavController(R.id.your_nav_host_fragment)
其中 R.id.nav_host_fragment
是你放在 NavHostFragment
上的 android:id
> 在您的活动布局中.
As per the Navigate to a destination documentation, you can use findNavController(R.id.your_nav_host_fragment)
where R.id.nav_host_fragment
is the android:id
you put on your NavHostFragment
in your Activity's layout.
- 转到编辑配置文件片段的操作.
为此,导航允许您设置全局操作- 图表中每个目的地都可用的操作.这是从您的 Activity 提供的 UI 触发操作的正确方法.
For this, Navigation allows you to set up global actions - an action that is available from every destination in your graph. This is the correct way of triggering actions from UI provided by your activity.
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_nav"
app:startDestination="@id/mainFragment">
...
<action android:id="@+id/action_global_editProfileFragment"
app:destination="@id/editProfileFragment"/>
</navigation>
当使用具有全局操作的安全参数,这将生成一个 MainNavDirections
类,其中包含您的操作.
When using Safe Args with a global action, this will generate a MainNavDirections
class that has your action on it.
这意味着您完成的点击侦听器将如下所示:
This means your completed click listener would look like:
headerOfNavDrawer.setOnClickListener{
// Use the Kotlin extension in the -ktx artifacts
val navController = findNavController(R.id.nav_host_fragment)
// Now use the generated Directions class to navigate to the destination
navController.navigate(MainNavDirections.actionGlobalEditProfileFragment())
}
这篇关于如何在导航架构中导航抽屉标题操作的任何目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!