我正在尝试使用 LazyVerticalGrid 显示分页项目。我正在尝试的代码如下所示。
val categories = repo.categories.collectAsLazyPagingItems()
LazyVerticalGrid(
cells = GridCells.Fixed(2),
modifier = Modifier.padding(8.dp)
) {
items(categories) { category ->
Box(Modifier.padding(8.dp)) {
CategoryView(category)
}
}
}
请注意,我已导入 androidx.paging.compose.items
和 androidx.paging.compose.collectAsLazyPagingItems
.还有categories
是 LazyPagingItems<Category>
类型.它与
LazyColumn
完美配合和 LazyRow
但不是 LazyVerticalGrid
.我得到的错误是:
Type mismatch.
Required:
Int
Found:
LazyPagingItems<Category>
最佳答案
我通过为 LazyGridScope
编写扩展函数想出了一个解决方案就像为 LazyListScope
写的那篇文章一样在 androidx.paging:paging-compose
图书馆。
@ExperimentalFoundationApi
public fun <T: Any> LazyGridScope.items(
lazyPagingItems: LazyPagingItems<T>,
itemContent: @Composable LazyItemScope.(value: T?) -> Unit
) {
items(lazyPagingItems.itemCount) { index ->
itemContent(lazyPagingItems[index])
}
}
关于android - 如何使用带有 LazyVerticalGrid 的 jetpack 组合分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68445699/