首页 > Java > LazyColumn 引用返回起始位置

LazyColumn 引用返回起始位置

WBOY
发布: 2024-02-10 09:27:07
转载
826 人浏览过

php小编百草今天为大家介绍一款非常实用的功能——LazyColumn 引用返回起始位置。在开发过程中,我们经常需要创建一个垂直滚动列表,而LazyColumn正是为此而生。它可以根据需要动态生成列表项,大大提高了应用的性能和效率。同时,LazyColumn还可以返回列表的起始位置,方便我们进行指定位置的操作和处理。无论是开发移动应用还是网页应用,LazyColumn都是一个非常实用的工具,让我们一起来了解一下吧!

问题内容

我有一个申请。一张大图片和底部的lazycolumn。当我转到第四个(或第五个)链接然后返回时,我的 lazycolumn 最终位于初始位置。链接1在最上面。当我再次想要链接 4 时,我必须“扭转”。

“本机”导航栏上的“后退”按钮有所不同。当我回来时,lazycolumn 已经以我留下的形式在等着我。

程序中需要做什么才能使顶部按钮的行为与导航栏上的“后退”按钮相同?

这是我的文件:

navgraph.kt

@composable
 fun navgraph(navcontroller: navhostcontroller){
     navhost(navcontroller = navcontroller, startdestination = screens.contents.route
     ) {
         addcontentsscreen(navcontroller, this)
         addscreen1(navcontroller, this)
         addscreen2(navcontroller, this)
         addscreen3(navcontroller, this)
         addscreen4(navcontroller, this)
         addscreen5(navcontroller, this)
     }
 }

 private fun addcontentsscreen(
     navcontroller: navhostcontroller,
     navgraphbuilder: navgraphbuilder
 ) {
     navgraphbuilder.composable(route = screens.contents.route) {
         contentsscreen(
             navigatescreen1 = { navcontroller.navigate(screens.screens1.route) },
             navigatescreen2 = { navcontroller.navigate(screens.screens2.route) },
             navigatescreen3 = { navcontroller.navigate(screens.screens3.route) },
             navigatescreen4 = { navcontroller.navigate(screens.screens4.route) },
             navigatescreen5 = { navcontroller.navigate(screens.screens5.route) },
         )
     }
 }

 private fun addscreen1(navcontroller: navhostcontroller, navgraphbuilder: navgraphbuilder) {
     navgraphbuilder.composable(route = screens.screens1.route) {
         screen1 (navigatecontentsscreen = { navcontroller.navigate(screens.contents.route) })
     }
 }

 private fun addscreen2(navcontroller: navhostcontroller, navgraphbuilder: navgraphbuilder) {
     navgraphbuilder.composable(route = screens.screens2.route) {
         screen2 (navigatecontentsscreen = { navcontroller.navigate(screens.contents.route) })
     }
 }

 private fun addscreen3(navcontroller: navhostcontroller, navgraphbuilder: navgraphbuilder) {
     navgraphbuilder.composable(route = screens.screens3.route) {
         screen3 (navigatecontentsscreen = { navcontroller.navigate(screens.contents.route) })
     }
 }

 private fun addscreen4(navcontroller: navhostcontroller, navgraphbuilder: navgraphbuilder) {
     navgraphbuilder.composable(route = screens.screens4.route) {
         screen4 (navigatecontentsscreen = { navcontroller.navigate(screens.contents.route) })
     }
 }

 private fun addscreen5(navcontroller: navhostcontroller, navgraphbuilder: navgraphbuilder) {
     navgraphbuilder.composable(route = screens.screens5.route) {
         screen5 (navigatecontentsscreen = { navcontroller.navigate(screens.contents.route) })
     }
 }
登录后复制

屏幕

sealed class screens(val route: string) {
     data object contents : screens("contents_screen")
     data object screens1 : screens("screen_1");
     data object screens2 : screens("screen_2");
     data object screens3 : screens("screen_3")
     data object screens4 : screens("screen_4");
     data object screens5 : screens("screen_5");
 }
登录后复制

screens.kt

class item(val link: () -> unit, val name: string)

 @composable
 fun contentslist(explanation: list<item>) {
     val liststate = rememberlazyliststate()
     lazycolumn(state = liststate) {
         items(explanation.size) { index ->
             val item = explanation[index]
             contentsbutton(
                 onclick = item.link,
                 text = item.name
             )
         }
     }
 }

 @composable
 fun contentsscreen(
     navigatescreen1: () -> unit,
     navigatescreen2: () -> unit,
     navigatescreen3: () -> unit,
     navigatescreen4: () -> unit,
     navigatescreen5: () -> unit,
 ) {
     val listofquestions = listof(
         item( navigatescreen1, "screen1"),
         item( navigatescreen2, "screen2"),
         item( navigatescreen3, "screen3"),
         item( navigatescreen4, "screen4"),
         item( navigatescreen5, "screen5"),
     )
     column {
         image(
             modifier = modifier.height(650.dp),
             painter = painterresource(id = r.drawable.cat),
             contentdescription = null,
             contentscale = contentscale.crop
         )
         contentslist(listofquestions)
     }
 }
登录后复制

screen4.kt

@OptIn(ExperimentalMaterial3Api::class)
 @Composable
 fun Screen4(
     navigateContentsScreen: () -> Unit,
 ) {
     val text = "Screen4"
     Scaffold(
         containerColor = lightGray,
         contentColor = darkGrey,
         topBar = {
             TopAppBar(
                 modifier = Modifier.height(80.dp),
                 title = {
                     Box(
                         Modifier.fillMaxHeight(),
                         contentAlignment = Alignment.Center
                     ) {
                         Text(
                             maxLines = 1,
                             overflow = TextOverflow.Ellipsis,
                             text = text,
                             color = orange,
                         )
                     }
                 },
                 navigationIcon = {
                     Box(
                         Modifier.fillMaxHeight(),
                         contentAlignment = Alignment.Center
                     ) {
                         IconButton(
                             onClick = navigateContentsScreen
                         ) {
                             Icon(
                                 imageVector = Icons.Filled.ArrowBack,
                                 contentDescription = "Back",
                                 tint=orange,
                             )
                         }
                     }
                 },
                 colors = TopAppBarDefaults.mediumTopAppBarColors(
                     containerColor = darkGrey
                 )
             )
         }
     ) { contentPadding ->
         Column(
             modifier = Modifier
                 .fillMaxSize()
                 .padding(contentPadding)
                 .verticalScroll(rememberScrollState()),
         ) {
             Text(
                 text = "Screen4",
                 color = darkGrey,
                 textAlign = TextAlign.Justify,
                 modifier = Modifier.padding(24.dp)
             )
         }
     }
 }
登录后复制

我尝试并搜索,但一无所获......

解决方法

尝试按如下方式更改您的代码:

navGraphBuilder.composable(route = Screens.Screens4.route) {
    Screen4 (navigateContentsScreen = { navController.navigateUp() })
}
登录后复制

当您使用 navcontroller.navigate(screens.contents.route) 时,您将始终为该路由创建一个新的 backstackentry ,并且这个新目的地将是全新创建的,没有任何状态。请小心,您将使用当前的方法建立一个巨大的后台堆栈。

使用 navcontroller.navigateup()navcontroller.popbackstack(),您将获得实际的先前目的地。请参阅此 stackoverflow 帖子了解差异的说明。

以上是LazyColumn 引用返回起始位置的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板