You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PagingData中LazyColumn的itemsIndexed已废弃,如何编写替代代码?

在PagingData结合LazyColumn时替代废弃的itemsIndexed方法

当使用Paging3的LazyPagingItems配合Jetpack Compose的LazyColumn时,原有的itemsIndexed用法已被废弃,可通过以下方式替换:

废弃的旧代码

val list = (state as State.Success).data.collectAsLazyPagingItems()
LazyColumn(
    modifier = Modifier
        .padding(top = 50.dp)
        .border(4.dp, MaterialTheme.colors.primary)
        .padding(padding)
) {
    itemsIndexed(list) {}
}

替代的新代码

val list = (state as State.Success).data.collectAsLazyPagingItems()
LazyColumn(
    modifier = Modifier.fillMaxSize()
) {
    items(
        count = list.itemCount,
        key = list.itemKey(),
        contentType = list.itemContentType()
    ) { index ->
        val item = list[index]
        // 编写原itemsIndexed逻辑中对应位置的UI代码
    }
}

关键说明

  • 改用items函数,通过list.itemCount传入列表总项数
  • 复用list.itemKey()和list.itemContentType(),保留Paging提供的项复用逻辑,保障列表性能
  • 通过index参数获取对应位置的item,替代原itemsIndexed的索引+item参数结构

内容的提问来源于stack exchange,提问作者Rafael Souza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 20:13:16