0

我目前正在研究可扩展卡的列表。我想关闭除当前选择的所有其他打开的可扩展卡。

我正在使用的数据是一个 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.
                        }
                    )
                }
            }
4

1 回答 1

0

您可以有一个通知当前打开卡的索引的状态:

val selectedIndexState = rememberSaveable {
        mutableStateOf(-1)
    }

然后将isExpanded参数添加到ExpandableCard

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
            }
        )
    }
}
于 2022-02-17T21:52:03.967 回答