Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 581
Olu Udeh Avatar asked Jul 19 '21 19:07

Olu Udeh


Video Answer


4 Answers

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])
    }
}
like image 60
Olu Udeh Avatar answered Oct 19 '22 21:10

Olu Udeh


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)
        }
    }
}
like image 23
Fahime Ghasemi Avatar answered Oct 19 '22 19:10

Fahime Ghasemi


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)
            }
    }
}
like image 3
Simsim Avatar answered Oct 19 '22 20:10

Simsim


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

 }
like image 3
Chunhui Deng Avatar answered Oct 19 '22 21:10

Chunhui Deng