android-jetpack-composeselectionhiddenlazycolumn

How to hide an item in a LazyColumn in Android Compose


How to hide an item in a LazyColumn in Android Compose?

I had a LazyColumn with more than a hundred items from which to select one.

Looking for a solution found the following Link1 and Link2.

But I felt that it was to complex to apply to my code.


Solution

  • I am using Jetpasck Compose so I don't use XML layouts.

    I found a solution for the case that the hidden Items follow a pattern or RegEx. Maybe yo can create an Extension function to make a custom made function to hide non pattern items.

    Within the LazyColumn Scope I added a filter to the List then in the selection code I do a lookup in the unfiltered List

    This a partial code using this option:

    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))
                        }
                    }
    
                }
    

    That is it every thing stays the same.