使用菜单按钮时的导航组件

Navigation Component when using Menu Buttons

我正在关注 this tutorial 以学习导航组件。 我测试工具栏菜单按钮导航 UI 的基本设置是

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
            android:id="@+id/detail_dest"
            android:icon="@drawable/ic_details"
            android:title="@string/details"
            app:showAsAction="ifRoom"/>

    <item
            android:id="@+id/settings_dest"
            android:icon="@drawable/ic_settings"
            android:title="@string/settings"
            app:showAsAction="ifRoom"/>


    <item
            android:id="@+id/camera_dest"
            android:icon="@drawable/ic_camera"
            android:title="@string/camera"
            app:showAsAction="ifRoom"/>
</menu>

导航图为

<?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/nav_graph"
            app:startDestination="@id/mainFragment">

    <fragment android:id="@+id/mainFragment"
              android:name="com.example.navmenu.MainFragment"
              android:label="fragment_main" tools:layout="@layout/fragment_main">

        <action android:id="@+id/action_mainFragment_to_detail_dest"
                app:destination="@id/detail_dest"
                app:enterAnim="@anim/slide_in_right"
                app:exitAnim="@anim/slide_out_left"
                app:popEnterAnim="@anim/slide_in_left"
                app:popExitAnim="@anim/slide_out_right"/>
    </fragment>

    <!-- Detail Fragment -->
    <fragment android:id="@+id/detail_dest"
              android:name="com.example.blankfragments.DetailFragment"
              android:label="fragment_detail" tools:layout="@layout/fragment_detail"/>

    <!-- Settings Fragment -->
    <fragment android:id="@+id/settings_dest"
              android:name="com.example.blankfragments.SettingsFragment"
              android:label="fragment_settings" tools:layout="@layout/fragment_settings"/>

    <fragment android:id="@+id/camera_dest"
              android:name="com.example.blankfragments.CameraFragment"
              android:label="fragment_camera" tools:layout="@layout/fragment_camera"/>
</navigation>

我的第一个问题是,当我触摸屏幕上的片段时,例如 SettingsFragment,当它已经在屏幕上时,它会被重新创建,这是否可以防止,如果可以,是否只能通过修改 xml 来防止?

第二个问题是教程说 menu.xml 中的项目 ID 应该与片段 ID 相同。 菜单中的 detail_dest 也应该是 nav_graph 中的 detail_dest。但是我希望使用动画完成过渡,因为它在您使用动作时会发生。当我更改带有 id 的使用菜单项时,它只转到细节片段一次并且没有动画,我该如何解决这个问题?

My first question is when i touch a fragment on screen, for instance SettingsFragment when it's already on screen it's re-created, can this be prevented, if so, is it possible only by modifying xml?

这是可以避免的。但是您不能从 xml 执行此操作。为此,您必须编写 kotlin 代码。

override fun onOptionsItemSelected(item: MenuItem): Boolean {

        val navController = findNavController(R.id.nav_host_fragment)
        val currentDestID = navController.currentDestination?.id

        return if(item.itemId == currentDestID){
            super.onOptionsItemSelected(item)
        }else{
            ((item.onNavDestinationSelected(navController))
                    || super.onOptionsItemSelected(item))
        }


}

这里,通过item.itemId == currentDestID这个条件,我检查了点击的菜单id是否与当前页面id相同。如果点击相同的 id 那么我刚刚调用了 super.

此解决方案仅适用于“选项”菜单

When i change use menu item with id it only goes once to Detail Fragment and without animation, how can i fix this?

从版本 2.2.0-alpha02 开始,当您使用 NavigationUI 时无法添加动画。可以查看源码here。他们已经初始化了默认动画。