Mohnish
03/04/2025, 3:12 PMmarlonlom
03/04/2025, 3:18 PMonGloballyPositioned
to get the height of each card.
* Keep track of the maximum height found so far.
2. Apply the maximum height:
* Use the Modifier.height
to set the height of all cards to the maximum height.
Here's a code example:
kotlin
@Composable
fun LazyRowWithUniformCardHeight(cardDataList: List<CardData>) {
var maxHeight by remember { mutableStateOf(0) }
LazyRow {
items(cardDataList) { cardData ->
Card(
modifier = Modifier
.onGloballyPositioned { coordinates ->
maxHeight = maxOf(maxHeight, coordinates.size.height)
}
.height(maxHeight.dp) // Apply the maximum height
) {
// Card content using cardData
}
}
}
}
Explanation:
* maxHeight
state variable stores the tallest card height.
* onGloballyPositioned
modifier measures each card's height and updates maxHeight
.
* Modifier.height(maxHeight.dp)
sets all cards to the tallest height.Mohnish
03/04/2025, 3:35 PMmaxHeight = maxOf(maxHeight, coordinates.size.height)
, maxHeight and coordinates.size.height both turn out to be zero, when you try to design it will see nothing on the screen as it forces height to take 0Arne Jans
03/05/2025, 8:35 AMLazyRow
-items
, you could instead use a Row
with height(IntrinsicSize.Min)
and fillMaxHeight()
on the Card
.
2. If that is not an option due to high item-count, you could use two different Card
-Composable, each with same fixed height, one for the big text, one for standard, and set the LazyRow
-height to wrap its content.
3. Or you could create your own custom-layout, which uses IntrinsicSize
, but I guess that wouldnt really offer an advantage, since SubcomposeLayout
does not support asking for intrinsic measurement, but maybe there would be another approach I am not thinking of possible here. https://developer.android.com/develop/ui/compose/layouts/intrinsic-measurementsMohnish
03/06/2025, 5:46 PMMohnish
03/06/2025, 5:47 PMLong Tran
03/23/2025, 6:42 PMMohnish
04/29/2025, 3:00 PMcartoonCard: @Composable (DailyAndAdditionGalleryCardData) -> Unit