在JetPack组合中搜索带有复选框选择的列表
当前有一个带有文本的懒惰列以及自定义复选框。目前,我可以使用以下代码在懒列中维护复选框选择。即使在滚动(上/下)期间,它也能够维护复选框选择的状态。
var baseCodes: MutableList<String> = arrayListOf()
val cmdList: List<MetaData> = mainViewModel.cmdList
val indexList: MutableList<Boolean> = MutableList(cmdList.size) { false }
LazyColumn() {
itemsIndexed(cmdList) { index: Int, cmd ->
MetaDataCard(
cmd = cmd,
navController = navController,
mainViewModel = mainViewModel,
indexList,
index
) {
if (it) {
baseCodes.add(cmd.code)
} else {
baseCodes.remove(cmd.code)
}
categoryViewModel.baseCodes = baseCodes
}
}
}
我已经用以下逻辑实施了搜索文本。
val cmdList: List<MetaData> = mainViewModel.cmdList
val filteredList: List<MetaData>
val searchText = textState.value.text
filteredList = if (searchText.isEmpty()) {
cmdList
} else {
val resultList = cmdList.filter { x ->
x.name!!.contains(searchText, true) || x.code.contains(searchText, true)
}
resultList
}
val indexList: MutableList<Boolean> = MutableList(filteredList.size) { false }
LazyColumn() {
itemsIndexed(filteredList) { index: Int, cmd ->
MetaDataCard(
cmd = cmd,
navController = navController,
mainViewModel = mainViewModel,
indexList,
index
) {
if (it) {
baseCodes.add(cmd.code)
} else {
baseCodes.remove(cmd.code)
}
categoryViewModel.baseCodes = baseCodes
}
}
}
元数据卡现在如下
fun MetaDataCard(
cmd: MetaData,
navController: NavController,
mainViewModel: MainViewModel,
completedList: MutableList<Boolean>,
index: Int,
processChange: (checked: Boolean) -> Unit
) {
val isSelected = rememberSaveable { mutableStateOf(completedList[index]) }
Card(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(8.dp)),
elevation = 10.dp,
backgroundColor = Color.White
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
verticalAlignment = Alignment.CenterVertically
)
{
Column() {
Text(
text = cmd.name,
color = Color.Gray,
style = Typography.body2
)
Text(
text = cmd.code,
Modifier.padding(0.dp),
color = MaterialTheme.colors.onPrimary,
style = Typography.body1,
)
}
Spacer(modifier = Modifier.weight(0.8f))
CustomCheckBox(
checked = isSelected.value,
onCheckedChange = {
isSelected.value = !isSelected.value
completedList[index] = !isSelected.value
processChange(isSelected.value)
},
)
}
}
}
如搜索选项,按照搜索框中的文本对列表进行过滤。 但是,现在我能够搜索和过滤文本,但是复选框选择没有保留,并且正在选择选择。
Currently there is a Lazy Column with Text along with Custom Checkbox. I am currently able to maintain the Checkbox selection in Lazy Column by using the following code. Even during scrolls (up/down) it is able to maintain the state of Checkbox selection.
var baseCodes: MutableList<String> = arrayListOf()
val cmdList: List<MetaData> = mainViewModel.cmdList
val indexList: MutableList<Boolean> = MutableList(cmdList.size) { false }
LazyColumn() {
itemsIndexed(cmdList) { index: Int, cmd ->
MetaDataCard(
cmd = cmd,
navController = navController,
mainViewModel = mainViewModel,
indexList,
index
) {
if (it) {
baseCodes.add(cmd.code)
} else {
baseCodes.remove(cmd.code)
}
categoryViewModel.baseCodes = baseCodes
}
}
}
I have implemented the Search with Text with Below Logic.
val cmdList: List<MetaData> = mainViewModel.cmdList
val filteredList: List<MetaData>
val searchText = textState.value.text
filteredList = if (searchText.isEmpty()) {
cmdList
} else {
val resultList = cmdList.filter { x ->
x.name!!.contains(searchText, true) || x.code.contains(searchText, true)
}
resultList
}
val indexList: MutableList<Boolean> = MutableList(filteredList.size) { false }
LazyColumn() {
itemsIndexed(filteredList) { index: Int, cmd ->
MetaDataCard(
cmd = cmd,
navController = navController,
mainViewModel = mainViewModel,
indexList,
index
) {
if (it) {
baseCodes.add(cmd.code)
} else {
baseCodes.remove(cmd.code)
}
categoryViewModel.baseCodes = baseCodes
}
}
}
The Metadata Card is as below
fun MetaDataCard(
cmd: MetaData,
navController: NavController,
mainViewModel: MainViewModel,
completedList: MutableList<Boolean>,
index: Int,
processChange: (checked: Boolean) -> Unit
) {
val isSelected = rememberSaveable { mutableStateOf(completedList[index]) }
Card(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(8.dp)),
elevation = 10.dp,
backgroundColor = Color.White
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
verticalAlignment = Alignment.CenterVertically
)
{
Column() {
Text(
text = cmd.name,
color = Color.Gray,
style = Typography.body2
)
Text(
text = cmd.code,
Modifier.padding(0.dp),
color = MaterialTheme.colors.onPrimary,
style = Typography.body1,
)
}
Spacer(modifier = Modifier.weight(0.8f))
CustomCheckBox(
checked = isSelected.value,
onCheckedChange = {
isSelected.value = !isSelected.value
completedList[index] = !isSelected.value
processChange(isSelected.value)
},
)
}
}
}
Now with Search option, the list gets filtered as per the text in the Search box.
However, now I am able to search and filter the text, but the checkbox selection is not retained and is messing up selection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
更改
,也可以简单地将任何列表转换为可变状态列表
Use
Change
Or you can simply convert any list to mutable state list
我已经使用了您的代码,这是我修改的方法以使其正常工作:
如果显示的元数据在选定的元数据中,则列出了所选元数据的列表,并在“行”设置中发出。
它也适用于搜索和滚动,
我对员工进行了修改
有趣的行(
员工:雇员,姓氏,
选择雇员:列表,
ProcessChange :(检查:布尔值) - &GT;单元
){
var iselected = Selected Employees.Contains(员工)
...
I've used your code and this is what I modified to make it work:
to Row Im sending list of selected MetaData and in Row im setting isSelected if the shown Metadata is in selected Metadatas.
it works also for searching and scrolling
i have it modified for employees
fun Row(
employee: EmployeeIdAndNameDTO,
selectedEmployees: List,
processChange: (checked: Boolean) -> Unit
) {
var isSelected = selectedEmployees.contains(employee)
...