使用Jetpack导航将自定义过渡动画添加到底部导航设置

aks*_*618 7 navigation android android-layout android-jetpack android-architecture-navigation

我正在使用Jetpack组件开发应用程序。如指南中所述,我用三个片段拼接了底部导航。但是,当按相应的导航按钮在片段之间切换时,我不知道如何更改过渡动画。

据我所知,有两种创建过渡的方法:

  • 将它们作为选项传递给navigate(),在这种情况下没有明确调用;
  • 使用具有动画属性的动作,但不知道如何告诉导航使用这些动作。也许给它一个特定的ID会工作吗?

因此,如何设置自定义过渡动画而不必放弃使用 BottomNavigation.setupWithNavController(navController)

unl*_*udo 6

我认为你不能,但会对解决方案感兴趣。

这是一个解决方法,如果有帮助的话:

不要将底部导航与导航控制器绑定(不要执行指南中指示的操作)。通过像这样设置处理程序来自己管理转换:

    bottomNav!!.setOnNavigationItemSelectedListener { item ->
        selectFragment(item)
        false
    }
Run Code Online (Sandbox Code Playgroud)

然后在每个片段之间创建转换并在处理程序中自己管理它们。这是一个带有 3 的示例:

private fun selectFragment(item: MenuItem) {
    if (selectedItem == -1)
        navController.navigate(item.itemId)
    else
        navController.navigate(
                when (item.itemId) {
                    R.id.interviewsFragment ->
                        if (selectedItem == R.id.personsFragment)
                            R.id.action_personsFragment_to_interviewsFragment
                        else
                            R.id.action_questionListsFragment_to_interviewsFragment
                    R.id.personsFragment ->
                        if (selectedItem == R.id.interviewsFragment)
                            R.id.action_interviewsFragment_to_personsFragment
                        else
                            R.id.action_questionListsFragment_to_personsFragment
                    R.id.questionListsFragment ->
                        if (selectedItem == R.id.interviewsFragment)
                            R.id.action_interviewsFragment_to_questionListsFragment
                        else
                            R.id.action_personsFragment_to_questionListsFragment
                    else -> item.itemId
                })

    selectedItem = item.itemId


    // uncheck the other items.
    for (i in 0 until bottomNav!!.menu.size()) {
        val menuItem = bottomNav!!.menu.getItem(i)
        if (menuItem.itemId == item.itemId) menuItem.isChecked = true
    }
}
Run Code Online (Sandbox Code Playgroud)

定义导航地图中的动画。这是一个包含 3 个片段的示例,动画向被选中的项目移动,因此感觉很自然:

<fragment
    android:id="@+id/interviewsFragment"
    android:name="com.unludo.interview.interview.list.InterviewsFragment"
    android:label="InterviewsFragment" >
    <action
        android:id="@+id/action_interviewsFragment_to_personsFragment"
        app:destination="@id/personsFragment"
        app:enterAnim="@anim/enter_from_right"
        app:exitAnim="@anim/exit_to_left" />
    <action
        android:id="@+id/action_interviewsFragment_to_questionListsFragment"
        app:destination="@id/questionListsFragment"
        app:enterAnim="@anim/enter_from_right"
        app:exitAnim="@anim/exit_to_left" />
</fragment>
<fragment
    android:id="@+id/personsFragment"
    android:name="com.unludo.interview.persons.list.PersonsFragment"
    android:label="PersonsFragment" >
    <action
        android:id="@+id/action_personsFragment_to_interviewsFragment"
        app:destination="@id/interviewsFragment"
        app:enterAnim="@anim/enter_from_left"
        app:exitAnim="@anim/exit_to_right" />
    <action
        android:id="@+id/action_personsFragment_to_questionListsFragment"
        app:destination="@id/questionListsFragment"
        app:enterAnim="@anim/enter_from_right"
        app:exitAnim="@anim/exit_to_left" />
</fragment>
<fragment
    android:id="@+id/questionListsFragment"
    android:name="com.unludo.interview.questions.lists.QuestionListsFragment"
    android:label="QuestionListsFragment" >
    <action
        android:id="@+id/action_questionListsFragment_to_personsFragment"
        app:destination="@id/personsFragment"
        app:enterAnim="@anim/enter_from_left"
        app:exitAnim="@anim/exit_to_right" />
    <action
        android:id="@+id/action_questionListsFragment_to_interviewsFragment"
        app:destination="@id/interviewsFragment"
        app:enterAnim="@anim/enter_from_left"
        app:exitAnim="@anim/exit_to_right" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

我认为这种行为可以由组件本身管理,但就目前而言,我认为我们必须手动管理。

干杯:)