简体   繁体   中英

LazyVerticalGrid for paging items in jetpack compose

I'm currently using paging items ( LazyPagingItems ) in LazyVerticalGrid via below extention function and it works fine.

inline fun <T : Any> LazyGridScope.items(
    items: LazyPagingItems<T>,
    crossinline itemContent: @Composable LazyGridItemScope.(item: T?) -> Unit
) {
    items(count = items.itemCount) { index ->
        itemContent(items[index])
    }
}

However, i want to make use of other parameters that LazyLazyout provides like key , span etc and tried below function.

inline fun <T : Any> LazyGridScope.items(
    items: LazyPagingItems<T>,
    noinline key: ((item: T?) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(item: T?) -> GridItemSpan)? = null,
    noinline contentType: (item: T?) -> Any? = { null },
    crossinline itemContent: @Composable LazyGridItemScope.(item: T?) -> Unit
) = items(
    count = items.itemCount,
    key = if (key != null) { index: Int -> key(items[index]) } else null,
    span = if (span != null) { { span(items[it]) } } else null,
    contentType = { index: Int -> contentType(items[index]) }
) {
    itemContent(items[it])
}

When i use that function with key , it shows errors that Type mismatch: inferred type is Long? but Any was expected Type mismatch: inferred type is Long? but Any was expected .

items(items = products, key = { product -> product?.productId }) {
//Content
}

I guess this is due to declaration of public class LazyPagingItems<T : Any> . How do I resolve it to use all parameters with paging items ?

With product?.productId you are trying to pass an optional Int? value, while key expects a non-optional Any value.

When you pass key = null to the computation block, under the hood it creates a unique key for each object depending on the index. A particular item key cannot be null as that would make it equal to other item keys, which is forbidden.

You can make call key only for non optional items, and provide index as the default value for optional case:

inline fun <T : Any> LazyGridScope.items(
    // ...
    noinline key: ((item: T) -> Any)? = null,
    // ...
) = items(
    // ...
    key = if (key != null) { index: Int -> items[index]?.let(key) ?: index } else null,
    // ...

Usage:

items(items = products, key = { product -> product.productId }) {

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