Last active
November 3, 2021 03:38
-
-
Save YektaDev/0cf3cda65d5cafce42fe0af0f99a6d6b to your computer and use it in GitHub Desktop.
This file contains 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
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