1

I try to use LazyHorizontalGrid with 2 fixed rows inside a LazyColumn. But then app crashes:

IllegalArgumentException: LazyHorizontalGrid's height should be bound by parent

This is surprising, as I can use LazyRow inside a LazyColumn without any problem.

mrzbn
  • 497
  • 1
  • 3
  • 15

1 Answers1

0

The LazyColumn wraps the height of the content and can be stretched and in this way the item scope has constraints.maxHeight = Constraints.Infinity. The sum of the rows' height in the LazyHorizontalGrid can't be Constraints.Infinity. It is the reason of the issue.

You have to assign a max height to your LazyHorizontalGrid.

LazyColumn {

    item {           
        LazyHorizontalGrid(
            rows = GridCells.Fixed(3),
            horizontalArrangement = Arrangement.spacedBy(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp),
            modifier = Modifier.heightIn(max = 200.dp)
        ) {
            // item content
        }
    }
    items(itemsList) {
        item content
    }
}

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • I cannot calculate height of LazyHorizontalGrid. And when I set a large max height (e.g. 2000.dp), LazyHorizontalGrid fills all that height. Still I don't understand, logically, what is the difference between 1 row (LazyRow) and multiple rows (LazyHorizontalGrid). – mrzbn Dec 31 '22 at 07:42
  • @mrzbn the `LazyHorizontalGrid` can't work in a container with `constraints.maxHeight = Constraints.Infinity`. The same happens with a scrollable Column. You can find the requirement here: https://github.com/androidx/androidx/blob/androidx-main/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridDsl.kt#L191 – Gabriele Mariotti Dec 31 '22 at 09:20