简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM