https://kotlinlang.org logo
#android
Title
# android
y

Yuniel

11/29/2023, 11:09 AM
Hi Everyone, I want to create a component (a dynamic row) with the last element Sticky (I want the last element to always be visible). I tried with this code but it doesn't work. Is that possible?
Copy code
@Composable
fun StickyLastItemLazyRow(items: List<String>) {
    val itemsWithSticky = items + List(1) { "Sticky" }

    LazyRow(
        content = {
            itemsWithSticky.forEachIndexed { index, item ->
                item {
                    if (index < items.size) {
                        Text(text = item, modifier = Modifier.padding(16.dp))
                    } else {
                        Spacer(modifier = Modifier.width(16.dp))
                        Text(text = item, modifier = Modifier.padding(16.dp))
                    }
                }
            }
        },
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.Gray)
    )
}

@Composable
fun StickyLastItemLazyRowExample() {
    val items = List(6) { "Item $it" }
    StickyLastItemLazyRow(items = items)
}