-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDearStairColumn.kt
46 lines (38 loc) · 1.47 KB
/
MyDearStairColumn.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package app.source.sample.customlayout
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Placeable
@Composable
fun MyDearStairColumn(
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
Layout(
content = content,
modifier = Modifier.then(modifier),
) { measurables, constraints ->
// map parameter List<Measurable> to List<Placeable>
val placeables: List<Placeable> = measurables.map { measurable ->
measurable.measure(constraints)
}
// calculate our layout width and height
val itemsTotalWidth = placeables.sumOf { placeable -> placeable.width }
val ourLayoutTotalWidth = if (itemsTotalWidth > constraints.maxWidth) constraints.maxWidth else itemsTotalWidth
val ourLayoutTotalHeight = placeables.sumOf { placeable -> placeable.height }
// place child items
layout(width = ourLayoutTotalWidth, height = ourLayoutTotalHeight) {
var y = 0
var x = 0
placeables.forEach { placeable ->
val itemHorizontalEndCoordinate = x + placeable.width
if (itemHorizontalEndCoordinate > constraints.maxWidth) {
x = 0
}
placeable.place(x = x, y = y)
y += placeable.height
x += placeable.width
}
}
}
}