Skip to content

Instantly share code, notes, and snippets.

@YektaDev
Last active November 3, 2021 03:38
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 YektaDev/0cf3cda65d5cafce42fe0af0f99a6d6b to your computer and use it in GitHub Desktop.
Save YektaDev/0cf3cda65d5cafce42fe0af0f99a6d6b to your computer and use it in GitHub Desktop.
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun <T> LazyGrid(
modifier: Modifier = Modifier,
items: List<T>,
rowSize: Int = 1,
gap: Dp = 0.dp,
itemContent: @Composable BoxScope.(T) -> Unit,
) {
val rows = items.chunked(rowSize)
LazyColumn(modifier) {
for ((rowIndex, row) in rows.withIndex()) {
val itemTopPadding = if (rowIndex == 0) gap else (gap / 2.dp).dp
val itemBottomPadding = if (rowIndex == rows.lastIndex) gap else (gap / 2.dp).dp
item {
Row(Modifier.fillParentMaxWidth()) {
for ((index, item) in row.withIndex()) {
val itemStartPadding = if (index == 0) gap else (gap / 2.dp).dp
val itemEndPadding = if (index == row.lastIndex) gap else (gap / 2.dp).dp
Box(
modifier = Modifier
.fillMaxWidth(1f / (rowSize - index))
.padding(top = itemTopPadding, bottom = itemBottomPadding, start = itemStartPadding, end = itemEndPadding)
) {
itemContent(item)
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment