【问题标题】:jetpack compose : creating LazyColumn with items parameterjetpack compose:使用 items 参数创建 LazyColumn
【发布时间】:2022-08-22 12:21:45
【问题描述】:
LazyColumn(modifier = Modifier.fillMaxWidth(), state = listState) {
    //ABC Category Items
    item {
        ABC(componentCoordinator = componentCoordinator)
    }

    //DEF Category Items
    item {
        DEF(coordinator = screenCoordinator)
    }

    //IJK Category Items
    item {
        IJK()
    }

    //XYZ Category Items
    item {
        XYZ(coordinator = screenCoordinator)
    }
}

@Composable
fun ABC(
    viewModel: ViewModel = hiltViewModel(),
    componentCoordinator: ComponentCoordinator
) {
    LazyRow(
        modifier = Modifier
            .fillMaxWidth()
            .height(64.dp),
        horizontalArrangement = Arrangement.SpaceEvenly,
    ) {
.........
})


所有 ABC、DEF、IJK、XYZ 都是具有 Column、Row、LazyRow 组合的可组合方法。

我必须将它们全部放在单个项目 {} 中,以便根据它们的索引分别跳转它们(使用 listState.animateScrollToItem(int))。现在,创建此 LazyColumn 的更好方法是使用带有这些可组合函数列表的 \"items\"(而不是 \"item\")参数。

像这样的东西:

LazyColumn(
        modifier = Modifier.fillMaxWidth(), state = listState
    ) {

        items(myItems, key = { it.uri }) { item ->
....
})

这个和 LazyColumn 函数体的数组初始化代码(因为这些方法没有相同的参数)可能是什么?

  • 您可以为每种项目类型创建一个枚举/密封类(在不同的内容数据的情况下),并传递项目数组,但我会说这种糖不会给您带来太多可读性,除非您需要更改动态排序。

标签: kotlin android-jetpack-compose lazycolumn


【解决方案1】:

怎么样

var itemsList = mutableStateListOf<@Composable (vararg : Any) -> Unit>)

存储所有可组合物

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-29
    • 2021-06-01
    • 2022-11-10
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2021-12-28
    相关资源
    最近更新 更多