如何在 Android Compose 中隐藏 LazyColumn 中的项目?
我有一个 LazyColumn,其中包含一百多个项目,可以从中选择一个。
但我觉得应用到我的代码中太复杂了。
我正在使用 Jetpasck Compose,因此我不使用 XML 布局。
我找到了针对隐藏项目遵循模式或正则表达式的情况的解决方案。也许您可以创建一个扩展函数来制作自定义函数来隐藏非模式项目。
在 LazyColumn 范围内,我向列表添加了一个过滤器,然后在选择代码中我在未过滤的列表中进行查找
这是使用此选项的部分代码:
LazyColumn(modifier = Modifier.fillMaxWidth(), state = listState) {
if (notSetLabel != null) {
item {
LargeDropdownMenuItem(
text = notSetLabel,
selected = false,
enabled = false,
onClick = { },
)
}
}
//Adding the filter. I my case a contains function was good enough
itemsIndexed(items.filter { x -> x.toString().contains(term, true)
}) { index, item ->
val selectedItem = index == selectedIndex
drawItem(
item,
selectedItem,
true
) {
//onItemSelected(index, item) --- Original code
//Change the selection to a lookup for original index in the unfiltered List
onItemSelected(items.indexOf(item), item)
expanded = false
}
if (index < items.lastIndex) {
Divider(modifier = Modifier.padding(horizontal = 8.dp))
}
}
}
就是这样,一切都保持不变。