kotlin 在android jetpack compose中使用ConstraintLayout的LazyColumn

zc0qhyus  于 2023-06-24  发布在  Kotlin
关注(0)|答案(2)|浏览(144)

我将在每个列的顶部放置一个标签,并在底部放置一个LazyСolumn。如下。

但是,当我填写它时,出现了我预期之外的屏幕:

还有什么需要我设置的吗?这是我的代码

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

qnakjoqk1#

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

sauutmhj2#

您忘记了LazyColumn的constrainAs

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

相关问题