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中文网其他相关文章!