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

Alexander Maryanovsky

02/16/2023, 9:13 PM
Is there an
Arrangement
that gives each item the same amount of “room” and then aligns within that room. Like if I put each item in a box of the same weight and aligned within the box?
a

Alex

02/16/2023, 11:17 PM
Do you mean
Arrangement.SpaceEvenly
?
a

Alexander Maryanovsky

02/17/2023, 9:45 AM
Unfortunately no.
SpaceEvenly
creates even spaces between the items, which is not the same.
r

Rafs

02/17/2023, 11:29 AM
Arrangement.spacedBy(dp, Alignment)
c

cb

02/17/2023, 11:46 AM
Arrangement
doesn’t affect the measurement of items, only placement.
a

Alexander Maryanovsky

02/17/2023, 11:47 AM
I understand that. Anyway, there doesn’t seem to be an arrangement like I want, so I wrote my own.
Copy code
private fun placeEqually(
    totalSize: Int,
    size: IntArray,
    outPosition: IntArray,
    reverseInput: Boolean
) {
    val itemRoom = totalSize / size.size
    var current = 0
    size.forEachIndexed(reverseInput) { index, it ->
        outPosition[index] = current + (itemRoom - it)/2
        current += itemRoom
    }
}
for centered alignment anyway
c

cb

02/17/2023, 11:49 AM
Ah I misread and thought this was another ‘have all items match the max size item’ 🤦‍♂️
Your code looks good. Could even pass in an alignment and use that to affect the item float
a

Alexander Maryanovsky

02/17/2023, 11:51 AM
The problem with all the existing arrangements is that when the size of one of the items changes, they all move around. So it doesn’t work if I want to display a row of numbers that are changing.
c

cb

02/17/2023, 11:52 AM
The movement isn’t necessarily a problem if you use
animateContentSize
a

Alexander Maryanovsky

02/17/2023, 11:53 AM
It is in my app 🙂
c

cb

02/17/2023, 11:54 AM
Fair enough. At least
Arrangement
exists and is easy to use for custom behavior
a

Alexander Maryanovsky

02/17/2023, 11:59 AM
Could be slightly easier if
IntArray.forEachIndexed(reversed: Boolean, action: (Int, Int) -> Unit)
wasn’t private
5 Views