'Is it possible to make LazyRow/LazyColumn display a certain amount of items on the screen without passing in an explicit DP width

Is there a way to make a LazyRow/Row display for example 2 items on the screen simultaneously without measuring the screen width manually and passing the DP width to the children of the LazyRow?

Current setup example:

LazyRow(
    modifier = Modifier
        .fillMaxWidth()
        .height(200.dp)
) {
    item {
        FirstItem(
            modifier = Modifier.width(
                ScreenUtils.getScreenWidthDP(LocalContext.current) / columnCount
            )
        )
    }
    item {
        SecondItem(
            modifier = Modifier.width(
                ScreenUtils.getScreenWidthDP(LocalContext.current) / columnCount
            )
        )
        )
    }
}


Solution 1:[1]

You can use Modifier.fillParentMaxWidth - it's available on LazyItemScope. For example, to display 2 items use fraction = 0.5f:

LazyRow(
    modifier = Modifier
        .fillMaxWidth()
        .height(200.dp)
) {
    item {
        FirstItem(
            modifier = Modifier
                .fillParentMaxWidth(0.5f)
        )
    }
    item {
        SecondItem(
            modifier = Modifier
                .fillParentMaxWidth(0.5f)
        )
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phil Dukhov