首页 > 解决方案 > 使用喷气背包导航将自定义过渡动画添加到底部导航设置

问题描述

我正在开发一个使用喷气背包组件的应用程序。如指南中所述,我将底部导航与三个片段缝合在一起。但是,我不知道在按下相应导航按钮时在片段之间切换时如何更改过渡动画。

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

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

标签: androidandroid-layoutnavigationandroid-jetpackandroid-architecture-navigation

解决方案


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

如果有帮助,这是一种解决方法:

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

    bottomNav!!.setOnNavigationItemSelectedListener { item ->
        selectFragment(item)
        false
    }

然后在每个片段之间创建转换并在处理程序中自己管理它们。以下是 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
    }
}

在导航地图中定义动画。这是一个包含 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>

我认为这种行为可以由组件本身管理,但现在,我认为我们必须手动管理。

干杯:)


推荐阅读