LazyColumn with ConstraintLayout in android jetpack compose
Asked Answered
H

2

5

I'm going to put a label on the top for each column and a LazyСolumn on the bottom. As follows.

enter image description here

However, when I filled it out, the screen other than I expected appeared:

enter image description here

Is there anything else I need to set up? Here is my code

@Composable
fun MenuDetailList(
    loading: Boolean,
    type: String,
    items: List<Any>,
    page: Int,
    onChangeScrollPosition: (Int) -> Unit,
    onTriggerNextPage: () -> Unit,
    onCallCacheDialog: (Int) -> Unit
) {
    val configuration = LocalConfiguration.current
    val screenHeight = configuration.screenHeightDp.dp

    Box(
        modifier = Modifier
            .background(color = MaterialTheme.colors.surface)
    ) {
        if (loading && items.isEmpty()) {
            LoadingShimmer(imageHeight = screenHeight)
        }else if (items.isEmpty()) {
            NothingHere()
        }else {
            ConstraintLayout(
                modifier = Modifier
                    .fillMaxSize()
                    .background(color = Color.White)
            ) {
                val (label, list) = createRefs()
                TopAppBar(
                    modifier = Modifier
                        .constrainAs(label) {
                            top.linkTo(parent.top)
                            start.linkTo(parent.start)
                            end.linkTo(parent.end)
                        }
                ) {
                    MenuDetailLabel()
                }
                LazyColumn(
                    modifier = Modifier
                        .background(color = Color.Blue)
                ) {
                    itemsIndexed(
                        items = items
                    ) { index, detail ->
                        onChangeScrollPosition(index)
                        if ((index + 1) >= (page * PAGE_SIZE) && !loading) {
                            onTriggerNextPage()
                        }
                        when(type) {
                            "purchase" -> {
                                PurchaseCard(
                                    purchase = detail as Purchase,
                                    onClick = onCallCacheDialog
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}
Haletta answered 18/11, 2021 at 0:21 Comment(2)
ConstraintLayout is redundant here. Just use Column, and add weight(1f) modifier to LazyColumn.Piatt
@PhilipDukhov Thanks, I just use constraint layout, it makes screen what I needHaletta
G
9
modifier = 
    Modifier.constrainAs(list) {
      top.linkTo(label.bottom)
      start.linkTo(parent.start)
      end.linkTo(parent.end)
      bottom.linkTo(parent.bottom)
    width = Dimension.fillToConstraints
    height = Dimension.fillToConstraints
}
Guardroom answered 20/4, 2022 at 7:11 Comment(3)
height give as Dimension.preferredWrapContentSylvester
@MuthukrishnanRajendran (preferredWrapContent) worked for me. thanksInterpose
Using height = Dimension.preferredWrapContent fixed this issue for me.Elwoodelwyn
K
0

You forgot the constrainAs for the LazyColumn:

LazyColumn(
    modifier = Modifier
        .background(color = Color.Blue)
        .constrainAs(list) {
            top.linkTo(label.bottom)
            start.linkTo(parent.start)
            end.linkTo(parent.end)
            bottom.linkTo(parent.bottom)
        }
)
Kalgan answered 18/11, 2021 at 5:28 Comment(2)
Thanks you for your answer, but it couldn't solve my problem. lazycolumn fill all screenHaletta
Well the fact that you were missing the constrainAs and adding it didn't help indicates that you have some even more issues. You should post a small demo app that shows everything.Kalgan

© 2022 - 2024 — McMap. All rights reserved.