android - 如何在jetpack compose中保存可扩展卡片组合状态和滚动位置
问题描述
我正在使用自定义可扩展卡,
@Composable
fun ExpandableCardComposable(
isExpandable: Boolean = false,
topContent: @Composable () -> Unit,
buttomContent: @Composable () -> Unit
) {
val transactionState = remember {
MutableTransitionState(isExpandable)
.apply {
targetState = isExpandable
}
}
val transaction = updateTransition( transactionState, label = "")
Card(
modifier = Modifier.padding(horizontal = Size.DOUBLE_SPACING),
elevation = Size.Card.ELEVATION,
shape = RoundedCornerShape(Size.Card.CORNER_RADIUS),
) {
Column(modifier = Modifier.animateContentSize()) {
Surface(elevation = Size.Card.ELEVATION) {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = { transactionState.targetState = !transaction.currentState })
.padding(horizontal = Size.DOUBLE_SPACING),
verticalAlignment = Alignment.CenterVertically
) {
Box(modifier = Modifier.weight(1f)) {
topContent()
}
val iconId = if (transaction.currentState) R.drawable.close else R.drawable.expand
Image(
imageVector = ImageVector.vectorResource(id = iconId),
contentDescription = null
)
}
}
if (transactionState.currentState) {
Box(
modifier = Modifier
.fillMaxWidth(),
) {
buttomContent()
}
}
}
}
}
我正在将此可组合用于包含数据列表及其工作的屏幕之一。但是,当我切换到其他屏幕并返回可扩展卡片屏幕时,卡片状态和滚动位置会发生变化。如何保存可扩展的卡片屏幕状态和滚动位置,使其不会改变。
解决方案
https://developer.android.com/jetpack/compose/navigation
保存和恢复状态是在导航期间完成的。如果您正在使用导航组件,请参阅saveState
和restoreState
。
推荐阅读
- amazon-web-services - 以编程方式共享 AWS 跨账户 RDS 快照的最佳实践
- pandas - pandas GroupBy agg 计算(传递参数)
- mysql - Mysql-我可以使用 group_concat 和“And”语句来实现这种结果吗
- c# - 打开文件对话框并使用 MediaInfo 库
- node.js - Mongoose,更新对象数组中的每个值
- css - .Less:在变量名中使用 & 和号
- strtol - 测试转换数据类型时的缓冲区问题
- javascript - 显示/隐藏内容列表中的文本
- python - 运行 U-Net 图像分割时出错
- sql - sqlalchemy 如何使用自定义列进行查询,该列给出带有条件的文字