我想首先概述一下我当前的应用程序设置。它利用
Jetpack Compose
并用 NavController
包装 AndroidViewBinding
,使我能够采用传统的导航方法,其中涉及大量导航图 XML 文件。这种方法背后的基本原理是将这些导航图迁移到 Jetpack Compose Navigation
(NavHost
,NavHostController
)需要付出巨大的努力。尽管如此,下面的实现仍然有效。
@Composable
private fun HfAppContent(
state: MainViewState,
eventHandler: (MainViewEvent) -> Unit
) {
AndroidViewBinding(
factory = FragmentNavHostBinding::inflate,
modifier = Modifier.fillMaxSize(),
onReset = {
}
) {
// Make sure initialising NavController only once.
if (state.navController == null) {
fragmentContainerView.getFragment<NavHostFragment>().navController.let {
// Set the NavController to view model
eventHandler(MainViewEvent.NavControllerInitialized(it))
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- DO NOT enable fitsSystemWindows which causes LoginScreen and FullScreenLoading show blank
status bar on logout somehow. Use Compose Modifier.imePadding instead -->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
但我遇到的唯一问题是,我发现在导航操作中定义
exitAnim
和 popExitAnim
不起作用,尽管 enterAnim
和 popEnterAnime
有效。
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_login"
app:startDestination="@+id/login">
<fragment
android:id="@+id/login"
android:name="com.myapp.features.login.view.LoginFragment">
<action
android:id="@+id/forgot_password_action"
app:destination="@+id/forgotPasswordEmail"
app:enterAnim="@anim/slide_in_from_right"
app:exitAnim="@anim/slide_out_to_left"
app:popEnterAnim="@anim/slide_in_from_left"
app:popExitAnim="@anim/slide_out_to_right" />
</fragment>
</navigation>
我想知道是否有人有类似的设置并面临同样的问题并知道如何解决它?
我有同样的情况,并通过以编程方式设置动画成功地使用动画制作动画。
val options = navOptions {
anim {
enter = R.anim.slide_in_right
exit = R.anim.slide_out_left
popEnter = R.anim.slide_in_left
popExit = R.anim.slide_out_right
}
}
view.findViewById<Button>(R.id.navigate_destination_button)?.setOnClickListener {
findNavController().navigate(R.id.flow_step_one_dest, null, options)
}