iroyo
11/07/2022, 3:19 PMiroyo
11/07/2022, 3:20 PM@Composable
fun TestItem(countText: Int ) {
Column(modifier = Modifier
.padding(16.dp)
.background(Color.Red)
) {
Box(
modifier = Modifier
.background(Color.Blue)
.height(128.dp)
.fillMaxWidth()
)
Spacer(modifier = Modifier.weight(1f))
repeat(countText) {
Text(
text = "This is a line",
)
}
}
}
Itemiroyo
11/07/2022, 3:20 PMLazyVerticalGrid(
cells = GridCells.Fixed(2),
) {
item {
TestItem(1)
}
item {
TestItem(4)
}
}
robercoding
11/07/2022, 4:52 PMModifier.aspectRatio(1f)
to the TestItem
Column
should work!iroyo
11/07/2022, 4:53 PMrobercoding
11/07/2022, 4:56 PMSubcomposeLayout
to measure each item, get the highest one, "return it" and set that height to each item?iroyo
11/07/2022, 5:00 PMrobercoding
11/07/2022, 5:06 PMrobercoding
11/07/2022, 5:08 PMrobercoding
11/07/2022, 5:24 PMrobercoding
11/07/2022, 6:43 PMdata class UiModel(val lines: Int)
@Composable
private fun MyCoolGrid(list: List<UiModel>) {
SubcomposeLayout { constraints ->
val measuredHeights = subcompose(SlotsEnum.Height) {
list.map { TestItem(countText = it.lines, minHeight = 0.dp) }
}.map {
val measure = it.measure(constraints)
val measuredHeight = measure.measuredHeight.toDp()
logD("measuredheight $measuredHeight")
measuredHeight
}
val maxHeight = measuredHeights.maxOrNull() ?: 0.dp // get the max height from all items!!
// Measure the LazyVerticalGrid
val contentPlaceable = subcompose(SlotsEnum.Content) {
LazyVerticalGrid(
modifier = Modifier,
columns = GridCells.Fixed(2),
) {
itemsIndexed(list) { index, item ->
TestItem(
text = "$maxHeight",
countText = item.lines,
minHeight = maxHeight // We are setting the max height to each item!!
)
}
}
}.first().measure(constraints)
logD("Whole LazyVerticalGrid component measured ${contentPlaceable.measuredHeight.toDp()}")
layout(contentPlaceable.width, contentPlaceable.height) {
// Place the LazyVerticalGrid with its content
contentPlaceable.place(0, 0)
}
}
}
private fun logD(text: String) {
Log.d("SomeCoolGrid", "$text")
}
private enum class SlotsEnum {
Height, Width, Content // You could also measure Width
}
@Composable
fun TestItem(text: String = "This is a line", countText: Int, minHeight: Dp = 0.dp) {
Column(
modifier = Modifier
.defaultMinSize(minHeight = minHeight)
.padding(horizontal = 16.dp, vertical = 16.dp)
.background(Color.Red)
) {
Box(
modifier = Modifier
.background(Color.Blue)
.height(128.dp)
.fillMaxWidth()
)
// Spacer(modifier = Modifier.weight(1f))
repeat(countText) {
Text(
text = "$text",
)
}
}
}