Last active
February 24, 2025 20:53
-
-
Save projectdelta6/08c837fa3672c8972bd410b4143e8f21 to your computer and use it in GitHub Desktop.
Androidx Paging 3 extensiopns for LazyList (LazyColumn and LazyRow) and LazyGrid. I had seen these `items(...)` extensions for Paging3 mentioned in a tutorial somewhere but then couldn't actually find them so I made my own.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//package ... | |
import androidx.compose.foundation.lazy.LazyItemScope | |
import androidx.compose.foundation.lazy.LazyListScope | |
import androidx.compose.foundation.lazy.grid.GridItemSpan | |
import androidx.compose.foundation.lazy.grid.LazyGridItemScope | |
import androidx.compose.foundation.lazy.grid.LazyGridItemSpanScope | |
import androidx.compose.foundation.lazy.grid.LazyGridScope | |
import androidx.compose.runtime.Composable | |
import androidx.paging.compose.LazyPagingItems | |
import androidx.paging.compose.itemContentType | |
import androidx.paging.compose.itemKey | |
/** | |
* Adds a list of items from a [LazyPagingItems] object. | |
* | |
* @param lazyPagingItems The [LazyPagingItems] object to use as the data source | |
* @param key a factory of stable and unique keys representing the item. Using the same key | |
* for multiple items in the list is not allowed. Type of the key should be saveable | |
* via Bundle on Android. If null is passed the position in the list will represent the key. | |
* When you specify the key the scroll position will be maintained based on the key, which | |
* means if you add/remove items before the current visible item the item with the given key | |
* will be kept as the first visible one. This can be overridden by calling | |
* 'requestScrollToItem' on the 'LazyListState'. | |
* @param contentType a factory of the content types for the item. The item compositions of | |
* the same type could be reused more efficiently. Note that null is a valid type and items of such | |
* type will be considered compatible. | |
* @param itemContent the content displayed by a single item | |
* @param placeholderItemContent the content displayed by a single placeholder item | |
* | |
* @see LazyListScope.items | |
*/ | |
inline fun <T: Any> LazyListScope.items( | |
lazyPagingItems: LazyPagingItems<T>, | |
noinline key: ((item: T) -> Any)? = null, | |
noinline contentType: (item: T) -> Any? = { null }, | |
crossinline itemContent: @Composable (LazyItemScope.(item: T) -> Unit), | |
crossinline placeholderItemContent: @Composable (LazyItemScope.() -> Unit) | |
) = items( | |
count = lazyPagingItems.itemCount, | |
key = if (key != null) lazyPagingItems.itemKey { key(it) } else null, | |
contentType = lazyPagingItems.itemContentType { contentType(it) } | |
) { | |
val item = lazyPagingItems[it] | |
if(item != null) { | |
itemContent(item) | |
} else { | |
placeholderItemContent() | |
} | |
} | |
/** | |
* Adds a list of items from a [LazyPagingItems] object. | |
* | |
* @param lazyPagingItems The [LazyPagingItems] object to use as the data source | |
* @param key a factory of stable and unique keys representing the item. Using the same key | |
* for multiple items in the list is not allowed. Type of the key should be saveable | |
* via Bundle on Android. If null is passed the position in the list will represent the key. | |
* When you specify the key the scroll position will be maintained based on the key, which | |
* means if you add/remove items before the current visible item the item with the given key | |
* will be kept as the first visible one. This can be overridden by calling | |
* 'requestScrollToItem' on the 'LazyListState'. | |
* @param span a factory of the span for the item. The span of the item could be changed | |
* dynamically based on the item data. If null is passed the span will be 1. | |
* @param contentType a factory of the content types for the item. The item compositions of | |
* the same type could be reused more efficiently. Note that null is a valid type and items of such | |
* type will be considered compatible. | |
* @param itemContent the content displayed by a single item | |
* @param placeholderItemContent the content displayed by a single placeholder item | |
* | |
* @see LazyGridScope.items | |
*/ | |
inline fun <T: Any> LazyGridScope.items( | |
lazyPagingItems: 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, | |
crossinline placeholderItemContent: @Composable (LazyGridItemScope.() -> Unit) | |
) = items( | |
count = lazyPagingItems.itemCount, | |
key = if (key != null) lazyPagingItems.itemKey { key(it) } else null, | |
span = if (span != null) { { span(lazyPagingItems.peek(it)) } } else null, | |
contentType = lazyPagingItems.itemContentType { contentType(it) } | |
) { | |
val item = lazyPagingItems[it] | |
if(item != null) { | |
itemContent(item) | |
} else { | |
placeholderItemContent() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've since expanded on this here adding more functionality