【发布时间】:2020-06-10 23:31:37
【问题描述】:
我有一个使用 Jetpack Compose 构建的带有 List-Detail 流程的应用程序。从详细信息视图返回到列表视图时,我想保留滚动位置。
根据模型状态交换表面:
@Composable
private fun AppContent() {
val scrollerPosition = ScrollerPosition()
Crossfade(State.currentScreen) { screen ->
Surface(color = MaterialTheme.colors.background) {
when (screen) {
is Screen.List -> ListScreen(scrollerPosition)
is Screen.Details -> DetailsScreen(screen.transaction)
is Screen.New -> NewScreen()
}
}
}
}
ListScreen 曾经有一个 VerticalScroller,我给它一个 ScrollerPosition 以在屏幕更改后保留位置。但是,此解决方案不适用于 AdapterList。
以前是这样的:
@Composable
private fun TransactionsList(modifier: Modifier, scrollerPosition: ScrollerPosition) {
Box(modifier = modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
VerticalScroller(scrollerPosition = scrollerPosition) {
AdapterList(State.transactions, itemCallback = { transaction ->
TransactionListRow(transaction)
ListDivider()
})
}
}
}
如何让 AdapterList 保持滚动位置?
@Composable
private fun TransactionsList(modifier: Modifier, scrollerPosition: ScrollerPosition) {
Box(modifier = modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
AdapterList(State.transactions, itemCallback = { transaction ->
TransactionListRow(transaction)
ListDivider()
})
}
}
【问题讨论】:
-
目前 (dev13) 这是不可能的,但是 compose 团队会在以后的版本中添加它。
标签: android kotlin android-jetpack-compose