如何导航导航架构中抽屉标题的任何目标 [英] How to Navigate any destination for action of header of drawer in navigation architecture

查看:120
本文介绍了如何导航导航架构中抽屉标题的任何目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都请解释一下,如何在导航架构中为抽屉的页眉布局定义动作.

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

  1. 进入编辑配置文件片段的动作.

为此,导航允许您设置全局动作-图表中每个目标位置都可以执行的操作.这是从您的活动提供的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>

在使用带有全局操作的安全Args时,这将生成一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆