Jetpack Compose 惰性列(LazyColumn)反向显示。

3

具有元素a、b和c的本机可组合列:

a

b

c

如何在Android Jetpack Compose中将排列方式反转为:

c

b

a


1
将传递给列合成器的项目列表反转。 - m0skit0
1个回答

10
你可以使用LazyColumnreverseLayout参数,如果你想保留集合的结构并且实现反向显示。
假如你有以下这些条目。
val items = listOf("Apple", "Banana", "Cherry", "Dogs", "Eggs", "Fruits")

ItemList(items = items)

只需将LazyColumnreverseLayout设置为true。
@Composable
fun ItemList(items: List<String>) {

    LazyColumn(
        reverseLayout = true
    ) {
        items(items) { item ->
            Box(modifier = Modifier
                .height(80.dp)
                .fillMaxWidth()
                .border(BorderStroke(Dp.Hairline, Color.Gray)),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = item
                )
            }
        }
    }
}

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接