[英]Adding custom transition animations to bottom navigation setup with jetpack navigation
我正在使用jetpack組件開發應用程序。 我按照指南中的描述用三個片段拼接了底部導航。 但是,在按下相應的導航按鈕切換片段時,我無法弄清楚如何更改過渡動畫。
據我所知,有兩種創建轉換的方法:
navigate()
選項傳遞,在這種情況下沒有顯式調用; 那么如何設置自定義過渡動畫而不必放棄使用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>
我認為這種行為可以由組件本身管理,但是現在,我認為我們必須手工管理。
干杯:)
我相信您可以通過創建R.anim.nav_default_ [Enter / Exit / PopEnter / PopExit]動畫文件的版本並將它們放在動畫資源目錄中來實現此行為。 然后,組件將使用這些文件作為默認動畫。 感覺它不應該像那樣工作,但目前它確實如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.