gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:18 26 4
gpt4 key购买 nike

我正在开发一个使用 Jetpack 组件的应用程序。如 guide 中所述,我用三个 fragment 拼接了底部导航。 .但是,我无法弄清楚如何在按下相应的导航按钮时在 fragment 之间切换时更改过渡动画。

据我所知,有两种创建转换的方法:

  • 将它们作为选项传递到 navigate() 中,在本例中未明确调用;
  • 使用带有动画属性的 Action ,但不知道如何告诉导航使用这些 Action 。也许给它一个特定的 ID 会起作用?

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

最佳答案

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

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

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

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

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

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

<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>

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

干杯:)

关于android - 使用 Jetpack 导航将自定义过渡动画添加到底部导航设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54344787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com