Skip to content

Instantly share code, notes, and snippets.

@ycmjason
Created April 20, 2024 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ycmjason/36a57dd4814f112ddb56fe69d0aae6cb to your computer and use it in GitHub Desktop.
Save ycmjason/36a57dd4814f112ddb56fe69d0aae6cb to your computer and use it in GitHub Desktop.
List item with divider
package app.fishball.taskshuffle.ui.components.contextless
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable
inline fun <T> LazyListScope.itemsWithDivider(
items: List<T>,
noinline divider: (@Composable LazyItemScope.(left: T, right: T) -> Unit),
noinline key: ((item: T) -> Any)? = null,
crossinline contentType: (item: T) -> Any? = { _ -> null },
crossinline itemContent: @Composable LazyItemScope.(item: T) -> Unit
) = itemsIndexed(
items,
key = if (key != null) { _, item -> key(item) } else null,
contentType = { _, item -> contentType(item) }
) { index, item ->
itemContent(item)
if (index < items.lastIndex) {
divider(this, items[index], items[index + 1])
}
}
//// USAGE
LazyColumn {
itemsWithDivider(
listOf("a", "b", "c"),
divider = { left, right -> HorizontalDivider() }) {
ListItem(
headlineContent = { Text(it) },
leadingContent = {
Icon(
Icons.Filled.Favorite,
contentDescription = "Localized description",
)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment