如何在抽屉布局(带有导航抽屉菜单)中使用Android导航组件(导航图)? [英] How to use the Android Navigation component (Nav Graph) in a Drawer Layout (with navigation drawer Menu)?

查看:83
本文介绍了如何在抽屉布局(带有导航抽屉菜单)中使用Android导航组件(导航图)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导航图,该导航图将此片段用作主活动XML中的主页.

I have a navigation graph that uses this fragment as a home in the main activity XML.

        <fragment
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        class="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/main_nav_graph"
        app:defaultNavHost="true"/>

我有一个带有菜单的Drawer布局,单击导航抽屉按钮时,我无法设法使导航正常工作(它可以从主要片段播放,但是当我单击Drawer按钮时,它不能工作),如果我使用使用getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new_fragment).commit();编程导航抽屉的旧方法,我的navcontroller丢失了!!我收到类似

I have a Drawer layout with a menu , I can't manage to make the navigation to work when I click on the navigation drawer button (it works from main fragment but not when I click on Drawer buttons), If I use the old way to program the navigation drawer using : getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new_fragment).commit();, my navcontroller is lost!! and I get errors like

navcontroller未知的目标片段,因为即使不是这种情况(从调试),控制器也会看到当前显示的home片段

destination fragment in unknown by navcontroller , because the controller will see the home fragment as currently displayed even if it is not the case (from debug)

public boolean onNavigationItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_1:
                //doesn't work when it current fragment doesnt match the 
                //action_fromfragmentx_to_fragmenty
                Navigation.findNavController(this,R.id.fragment_container)
                .navigate(R.id.action_fromfragmentx_to_fragmenty);
                break;

            //Other menu options...
        }

我的问题:我应该如何覆盖Java中的onNavigationItemSelected以使导航组件正常工作?有关此主题的任何链接或相对文档(使用Java)?.

推荐答案

导航"组件在使用用于导航抽屉的导航文档更新UI组件,您可以使用setupWithNavController()方法自动将菜单项连接到通过

The Navigation component offers a helper class in NavigationUI in the navigation-ui artifact. As per the Update UI components with Navigation documentation for navigation drawers, you can use the setupWithNavController() method to automatically hook up menu items to navigation destinations you set up in your navigation graph by tying the destination item to a menu item:

如果MenuItem的ID与目的地的ID相匹配,则NavController然后可以导航到该目的地.

If the id of the MenuItem matches the id of the destination, the NavController can then navigate to that destination.

因此,您根本不需要onNavigationItemSelected实现,也不需要执行任何FragmentTransactions.只需确保菜单XML中的android:id="@+id/fragment_y"与导航XML中的android:id="@+id/fragment_y"匹配,然后调用setupWithNavController():

Therefore you don't need a onNavigationItemSelected implementation at all, nor do you need to do any FragmentTransactions. Just make sure that the android:id="@+id/fragment_y" in your menu XML matches the android:id="@+id/fragment_y" in your navigation XML and call setupWithNavController():

NavigationView navView = findViewById(R.id.nav_view);
// This is what sets up its own onNavigationItemSelected
NavigationUI.setupWithNavController(navView, navController);

这篇关于如何在抽屉布局(带有导航抽屉菜单)中使用Android导航组件(导航图)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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