简体   繁体   English

如何使用jetpack compose导航到非底部栏屏幕?

[英]How to navigate to a non bottom bar screen with jetpack compose?

So i have an app in compose that has startDestination set as a screen with scaffold with bottom nav bar having 3 items and top app bar, i am able to navigate through the 3 bottom navigation tabs.因此,我在撰写中有一个应用程序,它的 startDestination 设置为带有脚手架的屏幕,底部导航栏有 3 个项目和顶部应用程序栏,我能够浏览 3 个底部导航选项卡。 But suppose i want to click on a card in one of the bottom tab screen which should open a details screen without the bottom bar and app bar (Since Navhost is inside the scaffold, bottom and top bars show up on details screen too) , what is the right way to do that?但是假设我想在一个底部标签屏幕中单击一张卡片,它应该打开一个没有底部栏和应用栏的详细信息屏幕(由于 Navhost 在脚手架内,底部和顶部栏也显示在详细信息屏幕上) ,什么这是正确的方法吗? Currently i have tried the following ways:-目前我尝试了以下方法:-

1. Starting the details screen in a new activity. 1. 在新活动中启动详细信息屏幕。

2. Using currentDestination route as state to conditionally hide bottom and app bar for details screen route. 2.以currentDestination路由为状态,有条件地隐藏底部和应用栏的详情屏幕路由。

Both work but, the problem with first approach is that, it isn't recommended to use multiple activity with jetpack navigation and rather we should stick to a single activity , it also further breaks the navigation if in case i want to move from details to another screen.两者都有效,但是第一种方法的问题是,不建议在喷气背包导航中使用多个活动,而是我们应该坚持一个活动,如果我想从细节转移到另一个屏幕。 The problem with second approach is that hiding and showing bottom/top bars create very bad transition between the screens and app doesn't feel smooth.第二种方法的问题是隐藏和显示底部/顶部栏会在屏幕之间产生非常糟糕的过渡,并且应用程序感觉不流畅。

Hence, i am looking for an appropriate way to handle this as per the guidelines , although i couldn't find any info on this.因此,我正在寻找一种根据指南处理此问题的适当方法,尽管我找不到任何有关此的信息。

Update更新

I went with showing/hiding the bottom and app bar based on routes, turns out that bad and laggy animation i was facing was because i was running a debug app, with a release app with minifyEnabled true (R8) ,the transitions are very smooth and natural, as i wanted them.我根据路线显示/隐藏底部和应用程序栏,结果发现我所面临的糟糕和滞后的动画是因为我正在运行一个调试应用程序,一个带有minifyEnabled true (R8) 的发布应用程序,过渡非常流畅和自然的,正如我想要的那样。

Also it turns out, as per Google's official compose sample app JetSnack , this is the appropriate way of achieving navigation from a bottombar screen to a non-bottombar screen.事实证明,根据 Google 的官方撰写示例应用程序JetSnack ,这是实现从底栏屏幕导航到非底栏屏幕的适当方式。 Thanks to @vitidev for pointing it out in the comments.感谢@vitidev在评论中指出这一点。

I had a similar problem and here's how I solved it我有一个类似的问题,这就是我解决它的方法

NavHost(xxx) {
    composable("AppScreen") {
        HorizontalPager(userScrollEnabled = false) {
            xxx
        }
    }
    composable("other screen") {
        xxx
    }
}

Use the accompanist Pager to maintain a screen with a bottomBar.使用伴奏寻呼机来维护一个带有底部栏的屏幕。 Setting userScrollEnabled in the pager and executing the interface jump through the pagerState is a good way to achieve navigation.在pager中设置userScrollEnabled,通过pagerState执行界面跳转,是实现导航的好方法。

onClick = {
    scope.launch {
        pagerState.scrollToPage(index)
    }
},

Then you can perform navigation jumps directly on the pager.然后就可以直接在寻呼机上进行导航跳转了。

navHost.navigate("other screen")

在此处输入图像描述

You can use a nested Navigation Graph:您可以使用嵌套导航图:

  • one principal graph according to your BottomBar根据您的 BottomBar 的一个主图
  • one nested graph inside each screen you want to go further每个屏幕内的一个嵌套图表,您想走得更远

https://developer.android.com/jetpack/compose/navigation#nested-nav https://developer.android.com/jetpack/compose/navigation#nested-nav

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

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