Home > Back-end >  Is it possible to make LazyRow/LazyColumn display a certain amount of items on the screen without pa
Is it possible to make LazyRow/LazyColumn display a certain amount of items on the screen without pa

Time:01-27

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
            )
        )
        )
    }
}

CodePudding user response:

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)
        )
    }
}

CodePudding user response:

I'm pretty sure you don't want to use LazyRow. And for Row no problem. Here's the code:

Row(
    modifier = Modifier
        .fillMaxWidth()
        .height(200.dp)
) {
        FirstItem(
            modifier = Modifier.weight(1f)
        )
        SecondItem(
            modifier = Modifier.weight(1f)
        )
}
  •  Tags:  
  • Related