简体   繁体   English

Android Compose:强制折叠所有展开的卡片,当前选中的卡片除外

[英]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.

相关问题 在expandable listview android中折叠除选定组之外的所有组 - Collapse all group except selected group in expandable listview android Recyclerview适配器仅保留所选项目,并删除除android中所选项目外的所有项目 - Recyclerview adapter keep selected item only and remove all items except selected in android 如何在Android中强制关闭当前正在运行的应用程序 - how to force close the currently running application in android 在 RecyclerView 中展开和折叠卡片 - Expand and Collapse cards in RecyclerView Android:以编程方式在列表视图中突出显示“当前选定的”选项 - Android: Highlight Currently Selected option in listview programmatically Android Spinner -- 如何调整当前选中项目的大小? - Android Spinner -- how to size to currently selected item? 如何在RecyclerView中展开和折叠卡片? - How to expand and collapse cards in RecyclerView? 有没有办法在Android中获取所有当前处于活动状态的应用程序? - Is there a way to get all currently active apps in Android? 三级可展开列表视图会折叠,但在第二组中除外 - Three level expandable list view collapse all but selected in second group 支持除 android 之外的所有平台的 Android 应用程序 - Android application with supporting all platforms except android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM