我正在使用 jetpack 導航組件來導航片段,我在 res 目錄中創建了一個影片并添加了以下內容animation-transition:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="600"
android:interpolator="@android:anim/decelerate_interpolator"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
并在視覺上將此影片設定為導航組件處打開的片段的輸入影片:

呼叫此后:
findNavController().navigate(R.id.reg2Fragment)
我仍然沒有得到我的手動影片,默認的淡入淡出顯示在那里。
我正在為我的整個專案主題使用 Material Io 設計。這是我的依賴項:
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.activity:activity-ktx:1.3.1"
implementation "androidx.fragment:fragment-ktx:1.3.6"
implementation platform('com.google.firebase:firebase-bom:28.4.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
// for Jet-Pack-Navigation-Component
implementation 'androidx.navigation:navigation-fragment-ktx'
implementation 'androidx.navigation:navigation-ui-ktx'
這是導航圖:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/nav_graph"
app:startDestination="@id/reg1Fragment">
<fragment
android:id="@ id/reg1Fragment"
android:name="com.example.authapp.uiRegistration.Reg1Fragment"
tools:layout="@layout/fragment_reg1"
android:label="Reg1Fragment" >
<action
android:id="@ id/action_reg1Fragment2_to_reg2Fragment"
app:destination="@id/reg2Fragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/slide_out_bottom"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
<fragment
android:id="@ id/reg2Fragment"
android:name="com.example.authapp.uiRegistration.Reg2Fragment"
tools:layout="@layout/fragment_reg2"
android:label="Reg2Fragment" >
<action
android:id="@ id/action_reg2Fragment_to_reg3Fragment"
app:destination="@id/reg3Fragment"
app:enterAnim="@anim/nav_default_enter_anim" />
</fragment>
<fragment
android:id="@ id/reg3Fragment"
android:name="com.example.authapp.uiRegistration.Reg3Fragment"
android:label="fragment_reg3"
tools:layout="@layout/fragment_reg3" >
<action
android:id="@ id/action_reg3Fragment_to_userProfile"
app:destination="@id/userProfile"
app:popUpTo="@id/userProfile" />
</fragment>
<activity
android:id="@ id/userProfile"
android:name="com.example.authapp.UserProfile"
android:label="activity_user_profile"
tools:layout="@layout/activity_user_profile" />
</navigation>
請任何專家幫助我解決這個問題。
謝謝
uj5u.com熱心網友回復:
findNavController().navigate(R.id.reg2Fragment)
這里方法的版本navigate(destinationId)不涉及影片,因為R.id.reg2Fragment除了目標片段 id 之外,它不提供任何資訊。
但是為了在導航中包含影片,您需要使用navigate() 方法,該方法接受一個navDirection引數,該引數通常是為此特定操作生成的Direction 類的方法。
所以,因為 idaction是action_reg1Fragment2_to_reg2Fragment:
<action
android:id="@ id/action_reg1Fragment2_to_reg2Fragment"
app:destination="@id/reg2Fragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/slide_out_bottom"
app:popExitAnim="@anim/slide_out_right" />
然后,導航將是:
findNavController().navigate(Reg1FragmentDirections.actionReg1Fragment2ToReg2Fragment())
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324309.html
標籤:安卓 android-fragments 导航 android-架构-组件 导航架构
