Home > Net >  Find the max height of an item in lazy row in compose
Find the max height of an item in lazy row in compose

Time:09-26

I have a lazy row with items that have wrap content height. my problem is that some texts of these items is visible or invisible base of that item. so the height of that cart is not fix.

how can I find the max height of that item (with all texts) and set that height to all items.

I do not want to set a hard code height to my items (like 300.dp)

as you can see in these images: the below button change its position based on the card's height. I want that button fix in its place and not move up and down. [1]: https://i.stack.imgur.com/R0Tc6.png

how can i find that?

CodePudding user response:

One of the rules of Compose is that you should only measure your children once; measuring children twice throws a runtime exception. However, there are times when you need some information about your children before measuring them.

If you have a look at the Android documentation for Intrinsic Measurements, you will have a clear idea of what to do. In your scenario, you need to force Compose to measure the size of your children to adjust the parent composable's dimensions accordingly.

This medium article gives an easier example on how to use IntrinsicSize in Compose.

CodePudding user response:

Have you tried experimenting around onGloballyPositioned{..} modifier property?

val localDensity = LocalDensity.current
                
Text(modifier = Modifier
           .onGloballyPositioned { thisText ->
               with (localDensity) {
                   thisText.size.height.toDp()
               }
           },
           text = "Text"
)

Edit: Intrinsic Measurement is not allowed in LazyList components, have a look at this post, looks like similar to yours.

Jetpack Compose: Row with all items same height

Also, Constraintlayout maybe a good thing to experiment on.

  • Related