https://kotlinlang.org logo
#compose
Title
# compose
k

Karthick

03/06/2024, 11:45 AM
I want to add a separator/Divider in a column after each item except last item, I don’t want to add more logics to check which one will be a last item. Any suggestions here?? For example i have 10 items inside my column but each can be added based a boolean condition, so anything can be the last item.
Compose is not inheritance based, so i couldn’t extend column and try a custom variant, and all the impl are internal
n

Nikolas Guillen Leon

03/06/2024, 11:52 AM
if it's a lazyColumn you can use itemsIndexed instead of items and wrap your divider in an if, something like this
Copy code
val myList = listOf()
itemsIndexed(items = myList) { index, item ->
    // item composable
    if (index < myList.lastIndex) {
        Divider()
    }
}
is it ok for your use case?
k

Karthick

03/06/2024, 11:54 AM
if i had list yes this is possible, Am thinking, because for my usecase i don’t want a expensive lazy column, just column is enough
s

Stylianos Gakis

03/06/2024, 12:29 PM
Put your composables in a list and iterate over them with the index 🤷‍♂️
n

Nikolas Guillen Leon

03/06/2024, 12:31 PM
Yup, I agree with @Stylianos Gakis, you can iterate your items with a forEachIndexed inside a regular column and use the same logic of my previous example
👍 1
13 Views