android - 使用喷气背包导航将自定义过渡动画添加到底部导航设置
问题描述
我正在开发一个使用喷气背包组件的应用程序。如指南中所述,我将底部导航与三个片段缝合在一起。但是,我不知道在按下相应导航按钮时在片段之间切换时如何更改过渡动画。
据我所知,有两种创建过渡的方法:
- 将它们作为选项传递给 ,
navigate()
在这种情况下没有被显式调用; - 使用带有动画属性的动作,但不知道如何告诉导航使用这些动作。也许给它一个特定的ID会起作用?
那么如何设置自定义过渡动画而不必放弃使用BottomNavigation.setupWithNavController(navController)
解决方案
我认为您不能,但会对解决方案感兴趣。
如果有帮助,这是一种解决方法:
不要将底部导航与导航控制器绑定(不要执行指南中指示的操作)。通过像这样设置处理程序来自己管理转换:
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>
我认为这种行为可以由组件本身管理,但现在,我认为我们必须手动管理。
干杯:)
推荐阅读
- sql - 如何使用 PostgreSQL 中的表中的 IN 获取唯一的最新项目
- python - 是否可以连接两个图表,一个用 matplotlib.pyplot 绘制,另一个用 mplfinance 绘制到同一个图中。例如; 相同的图像
- yaml - 如何在 YAML/Jekyll 中转义 < 和 >?
- mysql - 在 MySQL 8 中使用窗口函数获取不同列的计数
- angular - NgRx - 在路由器转换期间正在调度多个操作
- c# - 我可以在 Unity 中一次获得触摸位置和鼠标位置吗?
- java - Javadoc链接到默认包中的类?
- reactjs - 实现 Formik 但无法使用 react 建立正确的连接
- angular - Ionic - *ngFor 完成后的回调
- react-native - React Native:在将应用程序上传到播放商店之前初始化应用程序 json playstore url