【问题标题】:How is Android Studio Navigation Drawer navigating different Fragments?Android Studio Navigation Drawer 如何导航不同的 Fragment?
【发布时间】:2020-06-27 00:01:39
【问题描述】:

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

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


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

非常感谢。

【问题讨论】:

    标签: android android-studio android-layout android-fragments


    【解决方案1】:

    根据Update UI components with NavigationUI documentationsetupWithNavController() 方法将 UI 元素(例如您的 NavigationView)与 NavController 挂钩。

    看着setupWithNavController() Javadoc

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

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

    看看onNavDestinationSelected() Javadoc,菜单项和导航图目的地是如何匹配的就很清楚了:

    重要的是,它假定menu item id 与要导航到的有效action iddestination id 匹配。

    因此,单击带有android:id="@+id/nav_home" 的菜单项将导航到带有android:id="@+id/nav_home" 的目的地。

    【讨论】:

    • 感谢您的详细解释。真的很有帮助!
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多