Mario Adam
06/07/2023, 7:53 AMLazyVerticalGrid
getting the items centered horizontally. I want the items to be split per row starting from the centerMario Adam
06/07/2023, 7:54 AM5
)Mario Adam
06/07/2023, 7:54 AM@Composable
fun ParcelGrid(
modifier: Modifier = Modifier,
parcels: List<ParcelDto>,
userPermissions: List<Permission>,
showDeadline: Boolean,
removalEnabled: Boolean,
onRemoveParcel: (parcel: ParcelDto) -> Unit,
onTapParcel: (parcel: ParcelDto) -> Unit
) {
LazyVerticalGrid(
modifier = modifier,
columns = GridCells.Adaptive(minSize = 100.dp),
horizontalArrangement = Arrangement.Center,
verticalArrangement = Arrangement.Center
) {
items(parcels, { it.id }) {
ParcelGridItem(
parcel = it,
userPermissions = userPermissions,
showDeadline = showDeadline,
removalEnabled = removalEnabled,
onRemoveParcel = { onRemoveParcel(it) }) {
onTapParcel(it)
}
}
}
}
Alexander Zhirkevich
06/07/2023, 8:12 AMMario Adam
06/07/2023, 8:21 AMMario Adam
06/07/2023, 8:23 AMGridCells.Fixed(3)
instead of GridCells.Adaptive(minSize = 100.dp)
Stylianos Gakis
06/07/2023, 8:28 AMAlexander Zhirkevich
06/07/2023, 8:29 AMthere is space available in the third row…Point of fillMaxWidth is not to fill screen but set fixed constraints. It works with defult Column alignment so i thought it can also help with grid
Stylianos Gakis
06/07/2023, 8:32 AMitem
dsl takes in a span
lambda, where you can do exactly that.
It even has convenience values in there like maxCurrentLineSpan and maxLineSpan as specified here.
So then you can do like span = { GridItemSpan(this.maxLineSpan) }
for that last item, and it will take up 3 spaces automatically. Just need to make sure you pass it only to the correct item and not all of them so not all of them take 3 spaces 😄Mario Adam
06/07/2023, 8:35 AMhorizontalArrangement
would do? At least there is this question then: what is the purpose of horizontalArrangement
in `LazyVerticalGrid`then?Stylianos Gakis
06/07/2023, 8:37 AMMario Adam
06/07/2023, 8:38 AM100.dp
. Having my device in portrait mode, this makes up 3 items per row… but in landscape mode there would be like 5 or so…Mario Adam
06/07/2023, 8:38 AMStylianos Gakis
06/07/2023, 8:39 AMMario Adam
06/07/2023, 11:27 AMspan
wouldn’t work in this case then…Mario Adam
06/07/2023, 11:35 AMFlowRow(modifier = modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(4.dp, Alignment.CenterHorizontally), verticalAlignment = <http://Alignment.Top|Alignment.Top>) {
repeat(parcels.size) {
ParcelGridItem(
modifier = Modifier.padding(PaddingValues(bottom = 4.dp)),
parcel = parcels[it],
userPermissions = userPermissions,
showDeadline = showDeadline,
removalEnabled = removalEnabled,
onRemoveParcel = { onRemoveParcel(parcels[it]) }) {
onTapParcel(parcels[it])
}
}
}
Mario Adam
06/07/2023, 11:35 AMFlowRow
instead of LazyVerticalGrid
Stylianos Gakis
06/07/2023, 11:57 AMitem
be a full width spanning one, and have it contain more than 1 item inside there, have it render the entire last row for example, with 2 items in one item
lambda block or something like that. In any way, it’s possible but quite wacky 😄Stylianos Gakis
06/07/2023, 12:59 PMitem {}
.
It ain’t pretty, and it doesn’t handle non-fixed GridCells configuration, but maybe someone stumbles upon this and does require the laziness, so they can use this.
val items = remember {
List(23) {
Color((0xFF000000..0xFFFFFFFF).random())
}
}
val gridItems = remember(items) {
items.chunked(3)
}
LazyVerticalGrid(
columns = GridCells.Fixed(3),
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier.size(300.dp),
) {
for (gridItemChunk in gridItems.dropLast(1)) {
items(gridItemChunk) { item ->
YourItem(item)
}
}
item(span = { GridItemSpan(maxLineSpan) }) {
val lastItems = gridItems.last()
Row {
if (lastItems.size == 1) {
Spacer(Modifier.weight(1f))
YourItem(lastItems.first(), Modifier.weight(1f))
Spacer(Modifier.weight(1f))
} else {
Spacer(Modifier.weight(1f))
YourItem(lastItems[0], Modifier.weight(2f))
Spacer(Modifier.weight(1f))
YourItem(lastItems[1], Modifier.weight(2f))
Spacer(Modifier.weight(1f))
}
}
}
}
Also putting this here in case I need it in the future and don’t wanna think about it again 😅shikasd
06/07/2023, 4:34 PM