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

标签 android android-architecture-navigation

我正在关注 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>

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

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

最佳答案

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 .他们已经初始化了默认动画。

关于android - 使用菜单按钮时的导航组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440816/

相关文章:

android - 不允许设置设备所有者,因为设备上已经有多个用户

java - 使用 Scarlet 监听服务器响应

android - 使用 CollapsingToolbarLayout 时如何在 AppBarLayout 和滚动内容之间插入 LinearLayout

java - 没有像 postId 这样的 fb 我刚刚分享成功

Android 导航错误 : java. lang.IllegalArgumentException : navigation destination com. 示例---此 NavController 未知

Android 导航组件在 Activity 旋转时保留当前 Fragment

android - 是否可以将 androidx-navigation 与 onClick-databinding 一起使用,而无需在 fragment 中编写锅炉代码?

android - 查找动态创建的选定 img 的字符串值

android - 如何在单一 Activity 架构中使用 BottomNavigationView