【问题标题】:How to use jetpack compose paging with LazyVerticalGrid如何使用带有 LazyVerticalGrid 的 jetpack 组合分页
【发布时间】:2021-07-19 19:00:42
【问题描述】:

我正在尝试使用 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.itemsandroidx.paging.compose.collectAsLazyPagingItemscategories 的类型也是 LazyPagingItems<Category>

它与LazyColumnLazyRow 完美搭配,但不适用于LazyVerticalGrid。 我得到的错误是:

Type mismatch.
Required:
Int
Found:
LazyPagingItems<Category>

【问题讨论】:

    标签: android android-jetpack-compose android-paging-3


    【解决方案1】:

    我想出了一个解决方案,为LazyGridScope 编写了一个扩展函数,就像在androidx.paging:paging-compose 库中为LazyListScope 编写的函数一样。

    @ExperimentalFoundationApi
    public fun <T: Any> LazyGridScope.items(
        lazyPagingItems: LazyPagingItems<T>,
        itemContent: @Composable LazyItemScope.(value: T?) -> Unit
    ) {
        items(lazyPagingItems.itemCount) { index ->
            itemContent(lazyPagingItems[index])
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 2017-01-31
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多