Android Studio Navigation Drawer 如何导航不同的 Fragment? [英] How is Android Studio Navigation Drawer navigating different Fragments?

查看:22
本文介绍了Android Studio Navigation Drawer 如何导航不同的 Fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚 Android Studio 附带的默认导航抽屉活动模板是如何在不同片段之间导航的.我知道这个菜单是使用 AndroidX 导航组件和导航图的实现,但我无法理解每个菜单项如何映射到其相应的片段.我没有看到任何侦听器或 onNavigationItemSelected() 等.有人可以解释一下 menuItem 和相应片段之间的映射是如何实现的吗?

I've been trying to figure out how the default Navigation Drawer Activity template came with Android Studio navigates between different fragments. I understand that this menu is an implementation using AndroidX navigation component and navigation graph, but I just cannot understand how each menu item is mapped to its corresponding fragment. I don't see any listener or onNavigationItemSelected() etc. Can someone explain how the mapping between menuItem and corresponding fragment was achieved?

MainActivity.java:

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                navController.getGraph())
                .setDrawerLayout(drawer)
                .build();

        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

menu.xml:


    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <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/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
    </group>
</menu>

nav_graph.xml

nav_graph.xml


<?xml version="1.0" encoding="utf-8"?>
<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.buzzz.myapplication.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_HomeFragment_to_HomeSecondFragment"
            app:destination="@id/nav_home_second" />
    </fragment>
    <fragment
        android:id="@+id/nav_home_second"
        android:name="com.buzzz.myapplication.ui.home.HomeSecondFragment"
        android:label="@string/home_second"
        tools:layout="@layout/fragment_home_second">
        <action
            android:id="@+id/action_HomeSecondFragment_to_HomeFragment"
            app:destination="@id/nav_home" />

        <argument
            android:name="myArg"
            app:argType="string" />
    </fragment>

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.buzzz.myapplication.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.buzzz.myapplication.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />
</navigation>

非常感谢.

推荐答案

根据 使用 NavigationUI 文档更新 UI 组件setupWithNavController() 方法将 UI 元素(例如您的 NavigationView)与 NavController 挂钩.

As per the Update UI components with NavigationUI documentation, the setupWithNavController() methods hook up UI elements (such as your NavigationView) with the NavController.

查看 setupWithNavController() Javadoc:

Looking at the setupWithNavController() Javadoc:

设置一个 NavigationView 以与 NavController 一起使用.这将调用 onNavDestinationSelected 选择菜单项时.NavigationView 中的选中项会在目的地改变时自动更新.

Sets up a NavigationView for use with a NavController. This will call onNavDestinationSelected when a menu item is selected. The selected item in the NavigationView will automatically be updated when the destination changes.

所以在内部,这是设置适当的侦听器 - 在 NavigationView 上处理菜单选择和在 NavController 上在当前目标更改时更新所选项目.

So internally, this is setting up the appropriate listeners - both on the NavigationView to handle menu selections and on the NavController to update the selected item when the current destination changes.

查看onNavDestinationSelected() Javadoc,菜单项和导航图目的地的匹配方式变得清晰:

Looking at the onNavDestinationSelected() Javadoc, it becomes clear how the menu items and navigation graph destinations are matched:

重要的是,它假定 菜单项 id 匹配一个有效的action id要导航到的目的地 id.

Importantly, it assumes the menu item id matches a valid action id or destination id to be navigated to.

所以点击带有 android:id="@+id/nav_home" 的菜单项将导航到带有 android:id="@+id/nav_home" 的目的地代码>.

So clicking on a menu item with android:id="@+id/nav_home" will navigate to the destination with android:id="@+id/nav_home".

这篇关于Android Studio Navigation Drawer 如何导航不同的 Fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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