![](/img/trans.png)
[英]Collapse all group except selected group in expandable listview android
[英]Android Compose: Force Collapse All Expanded Cards except currently selected
我目前正在研究可擴展卡的列表。 我想關閉除當前選定的以外的所有其他打開的可擴展卡片。
我正在使用的數據是一個 Map<String, String>,但是我不知道如何獲取對列表中項目列表的引用以循環遍歷它並關閉所有其他期望當前的項目。 任何建議都會非常有幫助!
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.
}
)
}
}
你可以有一個 state 通知當前打開的卡的索引:
val selectedIndexState = rememberSaveable {
mutableStateOf(-1)
}
然后向ExpandableCard
添加一個isExpanded
參數:
ExpandableCard(
title = question.key,
description = question.value,
position = i++,
isExpanded = selectedIndexState.value == i,
onExpandableCardClicked = {
selectedIndexState.value = i
}
)
在處理這樣的列表時,您也可以考慮使用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.