https://kotlinlang.org logo
#compose
Title
# compose
c

Chris Johnson

09/02/2021, 7:03 PM
👋 Is there a way to measure a child before placing it and then fill the screen with the available amount of those children? Currently I'm creating a
Layout
for measurement of the child and then another
Layout
that will place X amount of those children based on screen height. The problem I ran into was each child I'm placing needs to be unique/not being currently laid out for the system to not give me
"Place was called on a node which was placed already"
.
z

Zach Klippenstein (he/him) [MOD]

09/02/2021, 7:04 PM
I think
SubcomposeLayout
might be your best bet here. You basically described how the lazy lists work.
☝️ 1
You can use it to subcompose+measure the first child, then subcompose+etc as many other children as you need.
c

Chris Johnson

09/02/2021, 7:36 PM
I thought about a
SubcomposeLayout
. Was sure that I would run into the same issue of not being able to place the same placeable. But now that I'm looking at it more calling
subcompose
would allow me to create a new placeable to place which should solve that. I'll try that next! Zach to the rescue again. 🦸
☝🏻 1
z

Zach Klippenstein (he/him) [MOD]

09/02/2021, 9:02 PM
yep exactly, you can just call
subcompose
multiple times with the same content function
c

Chris Johnson

09/02/2021, 9:14 PM
Sweet. That composable is so powerful
Now for the harder question of... which is better for performance? Having a
Layout
that gets the measure and is then removed from composition via a
mutableStateOf(Boolean)
+ a
Layout
to place. Or using
Subcomposelayout
Time to get out the profiler... 😱
z

Zach Klippenstein (he/him) [MOD]

09/02/2021, 9:34 PM
Before even considering performance, those will do different things. The former will not show the content until the second frame, which will most likely look noticeably bad.
c

Chris Johnson

09/02/2021, 9:50 PM
You don't really notice it, but yes you're right it would have to go through re-composition to show. Subcomposelayout seems to do the trick beautifully 🙂
z

Zach Klippenstein (he/him) [MOD]

09/02/2021, 10:19 PM
There can be other negative effects of skipping that first frame (e.g. it breaks the IDE preview)
🤔 1
also yes 1
3 Views