简体   繁体   English

如何在 JetpackCompose 的 LazyColumn 中 select 多个项目

[英]How to select multiple items in LazyColumn in JetpackCompose

How to select multiple items in LazyColumn and finally add the selected items in a seperate list.如何在 LazyColumn 中 select 多个项目并最终将所选项目添加到单独的列表中。

               GettingTags(tagsContent ={ productTags ->

                val flattenList = productTags.flatMap {
                    it.tags_list
                }
                Log.i(TAG,"Getting the flattenList $flattenList")

                LazyColumn{
                    items(flattenList){
                        ListItem(text = {Text(it) })
                        if(selectedTagItem) {
                            Icon(
                                imageVector = Icons.Default.Check,
                                contentDescription = "Selected",
                                tint = Color.Green,
                                modifier = Modifier.size(20.dp)
                            )
                        }
                    }
                }

            })

Mutable variable state可变变量 state

var selectedTagItem by remember{
    mutableStateOf(false)
}

First create a class with selected variable to toggle首先用选择的变量创建一个 class 来切换

@Immutable
data class MyItem(val text: String, val isSelected: Boolean = false)

Then create a SnapshotStateList via mutableStateListOf that contains all of the items, and can trigger recomposition when we update any item with new instance, add or remove items also.然后通过包含所有项目的 mutableStateListOf 创建一个 SnapshotStateList,并且可以在我们用新实例更新任何项目时触发重组,也可以添加或删除项目。 I used a ViewModel but it's not mandatory.我使用了 ViewModel 但这不是强制性的。 We can toggle items using index or get selected items by filtering isSelected flag我们可以使用索引切换项目或通过过滤isSelected标志来获取选定项目

class MyViewModel : ViewModel() {

    val myItems = mutableStateListOf<MyItem>()
        .apply {
            repeat(15) {
                add(MyItem(text = "Item$it"))
            }
        }

    fun getSelectedItems() = myItems.filter { it.isSelected }

    fun toggleSelection(index: Int) {

        val item = myItems[index]
        val isSelected = item.isSelected

        if (isSelected) {
            myItems[index] = item.copy(isSelected = false)
        } else {
            myItems[index] = item.copy(isSelected = true)
        }
    }
}

Create LazyColumn with key, key makes sure that only updated items are recomposed, as can be seen in performance document Create LazyColumn with key, key 确保只有更新的项目被重组,如性能文档中所示

@Composable
private fun SelectableLazyListSample(myViewModel: MyViewModel) {

    val selectedItems = myViewModel.getSelectedItems().map { it.text }
    Text(text = "Selected items: $selectedItems")
    LazyColumn(
        verticalArrangement = Arrangement.spacedBy(8.dp),
        contentPadding = PaddingValues(8.dp)
    ) {
        itemsIndexed(
            myViewModel.myItems,
            key = { _, item: MyItem ->
                item.hashCode()
            }
        ) { index, item ->
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.Red, RoundedCornerShape(8.dp))
                    .clickable {
                        myViewModel.toggleSelection(index)
                    }
                    .padding(8.dp)
            ) {
                Text("Item $index", color = Color.White, fontSize = 20.sp)
                if (item.isSelected) {
                    Icon(
                        modifier = Modifier
                            .align(Alignment.CenterEnd)
                            .background(Color.White, CircleShape),
                        imageVector = Icons.Default.Check,
                        contentDescription = "Selected",
                        tint = Color.Green,
                    )
                }
            }
        }
    }
}

Result结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 jetpackCompose 中对齐 LazyColumn 内的不同项目 - How can I align different items inside LazyColumn in jetpackCompose 我如何在项目中传递 2 个列表(LazyColumn) - How I can pass 2 lists in items (LazyColumn) 使用 JetpackCompose LazyColumn,Compose-Espresso 链接变为空闲超时 - Compose-Espresso link to become idle timed out With JetpackCompose LazyColumn 如何将多个图像上传到 FiresbaseStorage 并在 JetpackCompose 中取回 URL - How to Upload Multiple images to FiresbaseStorage and get back the urls in JetpackCompose 限制惰性列中的项目 - limit items in lazycolumn LazyColumn 不重新组合某些项目 - LazyColumn to not recompose some items 如何在 LazyColumn Jetpack Compose 中的项目之间添加分隔符? - How to add dividers between items in a LazyColumn Jetpack Compose? 如何在 Android Compose 中的 LazyColumn 项周围绘制边框 - How to draw border around the LazyColumn items in Android Compose Jetpack Compose:如何监听 lazycolumn 等列当前可见的项目? - Jetpack Compose: How to listen column currently visible items like lazycolumn? 如何检查 LazyColumn 中的项目对于屏幕高度是否太少? - How to check if the items in LazyColumn is too few for the height of the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM