Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add dividers between items in a LazyColumn Jetpack Compose?

I have a LazyColumn that looks like this:

LazyColumn (
    verticalArrangement = Arrangement.spacedBy(12.dp)
) {
    items(bookList) { book ->
        InProgressBookItem(book = book)
    }
}

How can I add a line between each item in the list, similar to using an item decoration on the old RecyclerView?

like image 975
cpgreen2 Avatar asked Apr 17 '21 15:04

cpgreen2


People also ask

How do you make a scrollable column in jetpack compose?

We can make the Column scrollable by using the verticalScroll() modifier.

What is recomposition in jetpack compose?

Recomposition is when Jetpack Compose re-executes the composables that may have changed in response to state changes, and then updates the Composition to reflect any changes. A Composition can only be produced by an initial composition and updated by recomposition.

What is side effects in jetpack compose?

A side-effect in Compose is a change to the state of the app that happens outside the scope of a composable function. For example, opening a new screen when the user taps on a button, or showing a message when the app doesn't have Internet connection.

Is jetpack easier to compose?

Jetpack Compose is a modern declarative UI Toolkit for Android. Compose makes it easier to write and maintain your app UI by providing a declarative API that allows you to render your app UI without imperatively mutating frontend views.


2 Answers

Currently there is no built–in way to add dividers. However, you can just add a Divider in the LazyListScope.

Something like:

LazyColumn(
    verticalArrangement = Arrangement.spacedBy(12.dp),
) {
    items(itemsList){
        Text("Item at  $it")
        Divider(color = Color.Black)
    }
}

If you do not want the last item to be followed by a Divider, you can add dividers to items according to their indices:

LazyColumn(
    verticalArrangement = Arrangement.spacedBy(12.dp),
) {
    itemsIndexed(itemsList) { index, item ->

        Text("Item at index $index is $item")

        if (index < itemsList.lastIndex)
            Divider(color = Color.Black, thickness = 1.dp)
    }
}

enter image description here

like image 137
Gabriele Mariotti Avatar answered Oct 10 '22 22:10

Gabriele Mariotti


Simple:

LazyColumn (
    verticalArrangement = Arrangement.spacedBy(12.dp)
) {
    items(bookList) { book ->
        InProgressBookItem(book = book)
        Divider(color = Color.Black, thickness = 1.dp)
    }
}
like image 11
Wilson Tran Avatar answered Oct 10 '22 22:10

Wilson Tran