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

Hamba

11/23/2023, 5:51 PM
for lazyverticalgrid, how can you set the spacing so that the columns stack up on the left and any excess spacing is taken up on the far right side? i want fixed width columns and as the grid is resized, if it becomes large enough to fit an extra column then the items will snap up to the right in the space. but i dont want the items gradually getting larger as the panel becomes larger. I tried limiting the overall width of the grid based on the number of columns it currently has, but then the space at the right is outside the element and i cant scroll it when my mouse is there.
i was able to do this by making a new GridCells class that just returned the desired column widths based on how many would fit into the width of the element.
Copy code
class StackLeft(private val colWidth: Dp) : GridCells {
    override fun Density.calculateCrossAxisCellSizes(
        availableSize: Int,
        spacing: Int
    ): List<Int> {
        val count = maxOf((availableSize + spacing) / (colWidth.roundToPx() + spacing), 1)
        return List(count) { colWidth.roundToPx() }
    }
}
Copy code
LazyVerticalGrid(
    columns = StackLeft(colWidth),
    ...
) {
    // contents
}
🙌 1
2 Views