简体   繁体   中英

How to use jetpack compose paging with LazyVerticalGrid

I am trying to display paged items using LazyVerticalGrid. The code I am trying is shown below.

 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)
          }
      }

 }

Please note that I have imported androidx.paging.compose.items and androidx.paging.compose.collectAsLazyPagingItems . Also categories is of type LazyPagingItems<Category> .

It works perfectly with LazyColumn and LazyRow but not LazyVerticalGrid . The error I am getting is:

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

I came up with a solution by writing an extension function for LazyGridScope like the one written for LazyListScope in the androidx.paging:paging-compose library.

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

For me the accepted answer didn't work. Instead of adding extension function I used the following way

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

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

copied from 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)
            }
    }
}

There is no need to create a extension function. Just use the other version of items function.

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])
          }
      }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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