简体   繁体   English

如何在导航架构中导航抽屉标题操作的任何目的地

[英]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??
}

You have two things you need:你有两件事需要:

  1. A reference to the NavController .NavController的引用。

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.按照该导航至目的地的文档,你可以使用findNavController(R.id.your_nav_host_fragment)其中R.id.nav_host_fragmentandroid:id ,你把你NavHostFragment在活动的布局。

  1. An action to go to the edit profile fragment.转到编辑配置文件片段的操作。

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.这是从您的活动提供的 UI 触发操作的正确方法。

<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>

When using Safe Args with a global action , this will generate a MainNavDirections class that has your action on it.Safe Args 与全局 action 一起使用时,这将生成一个MainNavDirections类,其中包含您的操作。

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())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何导航回导航抽屉 - How to Navigate back on a navigation Drawer 如果我使用导航组件单击导航抽屉中的菜单,如何执行操作而不是移动到另一个目的地? - how to perform an action instead of moving to another destination if I click a menu in Navigation Drawer using Navigation Component? 使用 PopUpTo 导航但不知道您的根目的地是什么 - 导航架构组件 - Navigate with PopUpTo but not knowing what you root destination is - Navigation Architecture Component 在带有标题的操作栏上创建导航抽屉 - Creating a navigation drawer over action bar with header 如何导航到导航抽屉项目上的活动单击 - How to navigate to activity on navigation drawer item click 如何通过导航抽屉导航到不同的活动 - How to navigate to different activities through the navigation drawer 如何同时使用导航抽屉和底部导航 - 导航架构组件 - How to use Navigation Drawer and Bottom Navigation simultaneously - Navigation Architecture Component 如何导航到Half Fragment? (导航架构组件) - How to navigate to Half Fragment ? (Navigation Architecture Component) 导航架构组件 - 导航抽屉 - Navigation Architecture Component - Navigation Drawer 如何使用导航抽屉添加自定义操作栏? - How to add custom action bar with navigation drawer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM