[英]Android Compose: Force Collapse All Expanded Cards except currently selected
I am currently working on a list of expandable cards.我目前正在研究可扩展卡的列表。 I would like to close all other opened expandable cards except the currently selected.我想关闭除当前选定的以外的所有其他打开的可扩展卡片。
The data I am working with is a Map<String, String>, however i don't know how to get reference to the list of items in the list to loop over it and close all others expect the current one.我正在使用的数据是一个 Map<String, String>,但是我不知道如何获取对列表中项目列表的引用以循环遍历它并关闭所有其他期望当前的项目。 Any suggestions would be very helpful!任何建议都会非常有帮助!
val size = viewModel.getQuestionsAndAnswers(groupName).size
Column {
var i = 0
for(question in viewModel.getQuestionsAndAnswers(groupName)) {
ExpandableCard(
title = question.key,
description = question.value,
position = i++,
onExpandableCardClicked = {
// question: how do I loop over all the items in expandable cards and close them.
}
)
}
}
You could have a state that informs the index of the currently opened card:你可以有一个 state 通知当前打开的卡的索引:
val selectedIndexState = rememberSaveable {
mutableStateOf(-1)
}
And then add a isExpanded
argument to the ExpandableCard
:然后向ExpandableCard
添加一个isExpanded
参数:
ExpandableCard(
title = question.key,
description = question.value,
position = i++,
isExpanded = selectedIndexState.value == i,
onExpandableCardClicked = {
selectedIndexState.value = i
}
)
You may also consider using LazyColumn
when working with lists like that:在处理这样的列表时,您也可以考虑使用LazyColumn
:
val selectedIndexState = rememberSaveable {
mutableStateOf(-1)
}
LazyColumn {
itemsIndexed(
items = viewModel.getQuestionsAndAnswers(groupName).toList()
) { index, question ->
ExpandableCard(
title = question.first,
description = question.second,
position = index,
isSelected = selectedIndexState.value == index,
onExpandableCardClicked = {
selectedIndexState.value = index
}
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.