简体   繁体   English

如何删除 Jetpack Compose Navigation 中的默认过渡

[英]How to remove default transitions in Jetpack Compose Navigation

I'm using the following sippet of code to navigate from a composable to another one, but it has a default fade animation.我正在使用以下代码片段从可组合导航到另一个,但它具有默认的淡入淡出 animation。 How can I remove it?我怎样才能删除它? I tried using an empty anim resource but it doesn't work.我尝试使用空的anim资源,但它不起作用。

navHostController.navigate(
    "destination_route",
    navOptions {
        popUpTo("this_route") {
            inclusive = true
        }
        anim {
            enter = R.anim.empty_animation
            exit = R.anim.empty_animation
            popEnter = R.anim.empty_animation
            popExit = R.anim.empty_animation
        }
    }
)

R.anim.empty_animation: R.anim.empty_animation:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Empty to disable animation-->
</set>

Currently, there is no way to configure the animations in the NavHost offered by Navigation-Compose's current version (2.4.0-beta02).目前,无法在 Navigation-Compose 当前版本(2.4.0-beta02)提供的 NavHost 中配置动画。

 @Composable public fun NavHost( navController: NavHostController, graph: NavGraph, modifier: Modifier = Modifier ) { val backStackEntry = visibleTransitionsInProgress.lastOrNull()?: visibleBackStack.lastOrNull() var initialCrossfade by remember { mutableStateOf(true) } if (backStackEntry,= null) { // while in the scope of the composable. we provide the navBackStackEntry as the // ViewModelStoreOwner and LifecycleOwner Crossfade(backStackEntry,id, modifier) { //// <<----- this

As Crossfade is not configurable, the transition cannot be changed.由于Crossfade不可配置,因此无法更改过渡。

To change the animation, you have to abandon using the NavHost provided by Navigation-Compose.要更改 animation,您必须放弃使用 Navigation-Compose 提供的NavHost

As of right now, as EpicPandaForce said it is not possible, but that is because it is in the works!截至目前,正如 EpicPandaForce 所说,这是不可能的,但那是因为它正在开发中!

Currently this functionality is served under accompanist, in the accompanist-navigation-animation artifact.目前,此功能由伴奏者提供,在伴奏者导航动画工件中。 You can read more about it here or in a more detailed blogpost here where they talk a bit about the future of it too.您可以在此处阅读更多相关信息,或者在此处阅读更详细的博客文章其中他们也谈到了它的未来。

The gist of it is that with that dependency (and without it when it eventually gets merged to the normal navigation-compose library) you will be able to write something like this:它的要点是,有了这个依赖项(当它最终合并到普通的 navigation-compose 库时没有它),你将能够编写如下内容:

composable(
  "profile/{id}",
  enterTransition = { _, _ ->
    // Whatever EnterTransition object you want, like:
    fadeIn(animationSpec = tween(2000))
  }
) { // Content }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM