Created
April 20, 2024 22:41
-
-
Save ycmjason/36a57dd4814f112ddb56fe69d0aae6cb to your computer and use it in GitHub Desktop.
List item with divider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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