小编Pet*_*cek的帖子

Jetpack Compose Paging - 使用 key 和 contentType

因此,在我的应用程序中,我正在使用 Compose Paging,但遇到了一个问题,我不确定如何正确处理。我来描述一下这个问题:

在我的分页列表中,我显示两种类型的项目:标题和实际数据单元格。我从 API 获取这些数据单元格的数据,并使用 insertSeparators 方法在它们之间插入标题。

我通过使用密封类来区分标题和单元格数据对象:

sealed class PagingModel {
    data class Header(@StringRes val label: Int) : PagingModel()
    data class CellData(val data: DataResponse) : PagingModel()
} 
Run Code Online (Sandbox Code Playgroud)

因此,在分页源中,我将 api 结果映射到PagingModel.CellDatainsertSeparators 中,并在 insertSeparators 中添加PagingModel.Header一些之前/之后的逻辑。

现在,说到问题。我希望分页列表尽可能高效,因此我想在 LazyColumn 中使用 key 和 contentType 参数。这是一个代码片段:

items(
    count = lazyPagingItems.itemCount,
    key = lazyPagingItems.itemKey { **What do I put here when PagingData.Header has no key** },
    contentType = lazyPagingItems.itemContentType {**should it just be "it" ?** }
  ) { index -> ...} …
Run Code Online (Sandbox Code Playgroud)

android android-paging android-jetpack-compose android-paging-3

4
推荐指数
1
解决办法
1108
查看次数