繁体   English   中英

如何使用带有 LazyVerticalGrid 的 jetpack 组合分页

[英]How to use jetpack compose paging with LazyVerticalGrid

我正在尝试使用 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.collectAsLazyPagingItems categories也是LazyPagingItems<Category>类型。

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

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

我想出了一个解决方案,为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])
    }
}

对我来说,接受的答案不起作用。 我没有添加扩展功能,而是使用了以下方式

val res = viewModel.getFeedResultStream().collectAsLazyPagingItems()

LazyVerticalGrid(columns = GridCells.Fixed(2)){
    items(res.itemCount)
    { index ->
        res[index]?.let {
            FeedItem(it)
        }
    }
}

从 androidx.paging:paging-compose 复制

fun <T : Any> LazyGridScope.items(
    items: LazyPagingItems<T>,
    key: ((item: T) -> Any)? = null,
    itemContent: @Composable LazyGridItemScope.(item: T?) -> Unit
) {
    items(
        count = items.itemCount,
        key = if (key == null) null else { index ->
            val item = items.peek(index)
            if (item == null) {
                PagingPlaceholderKey(index)
            } else {
                key(item)
            }
        }
    ) { index ->
        itemContent(items[index])
    }
}

@SuppressLint("BanParcelableUsage")
private data class PagingPlaceholderKey(private val index: Int) : Parcelable {
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(index)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object {
        @Suppress("unused")
        @JvmField
        val CREATOR: Parcelable.Creator<PagingPlaceholderKey> =
            object : Parcelable.Creator<PagingPlaceholderKey> {
                override fun createFromParcel(parcel: Parcel) =
                    PagingPlaceholderKey(parcel.readInt())

                override fun newArray(size: Int) = arrayOfNulls<PagingPlaceholderKey?>(size)
            }
    }
}

无需创建扩展功能。 只需使用其他版本的items功能。

val categories = repo.categories.collectAsLazyPagingItems()

 LazyVerticalGrid(
    cells = GridCells.Fixed(2),
    modifier = Modifier.padding(8.dp)
 ) {
      items(
        categories.itemCount
      ) { index ->
          Box(Modifier.padding(8.dp)) {
              CategoryView(categories[index])
          }
      }

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM