LazyColumn 错误:需要 LazyPagingItems<TypeVariable(T)> 但找到 List<T>

Raj*_*nan 1 android android-paging android-jetpack-compose android-paging-3 android-jetpack-compose-lazy-column

我正在使用该Paging 3库,LazyColumn并且我想根据购物清单项目的类别对列表进行排序。在下面的代码中,LazyColumn抱怨它正在期待LazyPagingItems<TypeVariable(T)>items属性,但发现了List<ShoppingListItem?>。我怎样才能解决这个问题?

可组合的

val lazyListState = rememberLazyListState()
val successItems = allItemsState.allItems?.collectAsLazyPagingItems()

LazyColumn(
    state = lazyListState,
    modifier = Modifier
        .fillMaxWidth(),
    contentPadding = PaddingValues(
        start = 5.dp,
        end = 5.dp,
        top = 8.dp,
        bottom = 165.dp
    ),
    verticalArrangement = Arrangement.spacedBy(5.dp),
) {
    val groupedByCategory = successItems!!.itemSnapshotList.groupBy { it!!.category }

    groupedByCategory.forEach { (initial, shoppingListItems) ->
        item {
            Text(text = initial)
        }
        items(
            items = shoppingListItems, //Throws error at this line
            key = { item ->
                item.id
            }
        ) { item ->
            ShoppingListScreenItem(
                item = item,
                mainViewModel = shoppingListScreenViewModel,
                onNavigateToAddEditItemScreenFromItemStrip = { shoppingListItem ->
                    onNavigateToAddEditItemScreenFromItemStrip(shoppingListItem)
                },
            ) { isChecked ->
                scope.launch {
                    shoppingListScreenViewModel.changeItemChecked(
                        item,
                        isChecked
                    )
                }
            }
            Divider(color = Color.LightGray, thickness = 1.dp)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误信息

Type mismatch.

Required:
LazyPagingItems<TypeVariable(T)>
Found:
List<ShoppingListItem?>
Run Code Online (Sandbox Code Playgroud)

V M*_*can 5

items(LazyPagingItems)函数已从较新版本的库中删除,这可能就是您无法找到导入的原因。这样做是为了使 API 更加灵活并支持所有类型的LazyContainers.

根据此处的发行说明:Paging Compose Version 1.0.0-alpha20,以下是应该使用的方法:

val lazyPagingItems = pager.collectAsLazyPagingItems()

LazyVerticalGrid(columns = GridCells.Fixed(2)) {
  // Here we use the standard items API
  items(
    count = lazyPagingItems.itemCount,
    // Here we use the new itemKey extension on LazyPagingItems to
    // handle placeholders automatically, ensuring you only need to provide
    // keys for real items
    key = lazyPagingItems.itemKey { it.uniqueId },
    // Similarly, itemContentType lets you set a custom content type for each item
    contentType = lazyPagingItems.itemContentType { "contentType" }
  ) { index ->
    // As the standard items call provides only the index, we get the item
    // directly from our lazyPagingItems
    val item = lazyPagingItems[index]
    PagingItem(item = item)
  }
}
Run Code Online (Sandbox Code Playgroud)