我正在开发一个使用 jetpack 组件的应用程序。我用三个片段缝合了底部导航,如guide https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#bottom_navigation。但是,我不知道在按下相应的导航按钮时在片段之间切换时如何更改过渡动画。
据我所知,有两种创建过渡的方法:
- 将它们作为选项传递
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>
我认为这种行为可以由组件本身来管理,但现在,我认为我们必须手动管理。
干杯:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)