android - 使用导航架构组件时出现错误的动画

标签 android androidx android-jetpack android-architecture-navigation

我正在构建一个使用 BottomNavigationView 和导航架构组件 ( https://github.com/fkshiba/POCNavigation ) 的应用程序。 每个选项卡都有一个 graph.xml,选项卡之间的转换由 Activity 完成。由于多重后退堆栈,没有导航 AC issue .我在 home_graph.xml 上有一个带有过渡动画的 Action 从 Home2 到 Home3。 问题是,一旦我运行此转换并弹出回到 Home2,然后导航到另一个选项卡。当我从另一个选项卡转换时,即使没有为其指定任何动画,它也会再次为 Home2 fragment 运行弹出动画。

有人知道这个问题的解决方案吗?

最佳答案

我在 GitHub 上看到了您的代码,我认为您错误地使用了 Android 导航。首先,您不需要为创建不同的图表(在您的情况下是四个) 不同的 fragment 。只需创建一个图表并在其中添加您必须在底部导航和其他事务中使用的所有 fragment 。 其次,您不需要手动执行 fragment 事务(在您的 Activity 代码中),您可以但不是必需的。 您更新的代码: 只有一张图:

<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/home_graph"
app:startDestination="@id/home2Fragment">
<fragment
    android:id="@+id/home2Fragment"
    android:name="com.felipeshiba.pocnavigation.Home2Fragment"
    android:label="fragment_home2"
    tools:layout="@layout/fragment_home2" >
    <action
            android:id="@+id/action_home2Fragment_to_home3Fragment"
            app:destination="@id/home3Fragment"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"/>
</fragment>
<fragment
    android:id="@+id/home3Fragment"
    android:name="com.felipeshiba.pocnavigation.Home3Fragment"
    android:label="fragment_home3"
    tools:layout="@layout/fragment_home3" >
    <deepLink
        android:id="@+id/deepLink"
        app:uri="ifood://home/" />
</fragment>
<fragment
    android:id="@+id/orders2Fragment"
    android:name="com.felipeshiba.pocnavigation.Orders2Fragment"
    android:label="fragment_orders2"
    tools:layout="@layout/fragment_orders2" />
<fragment
    android:id="@+id/profile2Fragment"
    android:name="com.felipeshiba.pocnavigation.Profile2Fragment"
    android:label="fragment_profile2"
    tools:layout="@layout/fragment_profile2" />
<fragment
    android:id="@+id/search2Fragment"
    android:name="com.felipeshiba.pocnavigation.Search2Fragment"
    android:label="fragment_search2"
    tools:layout="@layout/fragment_search2" /></navigation>

您更新的 Activity 代码:

class NavigationActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_navigation)
    val host: NavHostFragment = supportFragmentManager
            .findFragmentById(R.id.navigation_container) as NavHostFragment? ?: return

    val navController = host.navController
    bottom_navigation.setupWithNavController(navController)

}}

现在您可以通过将菜单项 ID 替换为 fragment ID 来调用菜单中的按钮导航项( fragment ),如下所示: 更新的 menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/home2Fragment"
    android:icon="?android:attr/actionModeCopyDrawable"
    android:title="Home" />

<item
    android:id="@+id/search2Fragment"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search" />

<item
    android:id="@+id/orders2Fragment"
    android:icon="?android:attr/actionModePasteDrawable"
    android:title="Orders" />

<item
    android:id="@+id/profile2Fragment"
    android:icon="?android:attr/actionModeCutDrawable"
    android:title="Profile" /></menu>

你的 activity.xml:

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavigationActivity">

<fragment
    android:id="@+id/navigation_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:visibility="gone"
    app:defaultNavHost="true"
    app:navGraph="@navigation/home_graph"
    app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/bottom_navigation"></android.support.constraint.ConstraintLayout>

对于 list 检查等其他更新(工作)代码 here

关于android - 使用导航架构组件时出现错误的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497215/

相关文章:

android - 防止菜单在触发 onItemClicked 后立即关闭

android - 从媒体 Controller 中删除后退和前进按钮

android - 如何在不使用绝对布局的情况下正确定位android布局?

android - 强制关闭应用程序后,工作经理定期工作无法在中文 ROM 上工作

android - 什么是 AndroidX.Test 框架,它如何影响我的单元/robolectric/espresso 测试?

android - 什么是 Android CameraX?

android - NestedScrollView 滚动滞后,直到 CollapsingToolbarLayout View 不折叠

android - 导航架构组件 - 对话框 fragment

android - AndroidX 中的 BottomSheetBehavior