Page
data class Page<Key : Any, Value : Any> : PagingSource.LoadResult<Key, Value>
kotlin.Any | ||
↳ | androidx.paging.PagingSource.LoadResult<Key, Value> | |
↳ | androidx.paging.PagingSource.LoadResult.Page |
Success result object for PagingSource.load.
// One common method of pagination is to use next (and optionally previous) tokens. // The below code shows you how to data class NetworkResponseObject( val items: List<Item>, val next: String, val approximateItemsRemaining: Int ) // The following shows how you use convert such a response loaded in PagingSource.load() to // a Page, which can be returned from that method fun NetworkResponseObject.toPage() = LoadResult.Page( data = items, prevKey = null, // this implementation can only append, can't load a prepend nextKey = next, // next token will be the params.key of a subsequent append load itemsAfter = approximateItemsRemaining )
// If you load by page number, the response may not define how to load the next page. data class NetworkResponseObject( val items: List<Item> ) // The following shows how you use the current page number (e.g., the current key in // PagingSource.load() to convert a response into a Page, which can be returned from that method fun NetworkResponseObject.toPage(pageNumber: Int): LoadResult.Page<Int, Item> { return LoadResult.Page( data = items, // Since 0 is the lowest page number, return null to signify no more pages // should be loaded before it. prevKey = if (pageNumber > 0) pageNumber - 1 else null, // This API defines that it's out of data when a page returns empty. When out of // data, we return `null` to signify no more pages should be loaded // If the response instead nextKey = if (items.isNotEmpty()) pageNumber + 1 else null ) }
Summary
Constants | |
---|---|
const Int |
Public constructors | |
---|---|
Success result object for PagingSource.load. |
|
<init>(data: List<Value>, prevKey: Key?, nextKey: Key?, @IntRange(-2147483648) itemsBefore: Int = COUNT_UNDEFINED, @IntRange(-2147483648) itemsAfter: Int = COUNT_UNDEFINED) Success result object for PagingSource.load. |
Properties | |
---|---|
List<Value> |
Loaded data |
Int |
Optional count of items after the loaded data. |
Int |
Optional count of items before the loaded data. |
Key? |
Key for next page if more data can be loaded in that direction, |
Key? |
Key for previous page if more data can be loaded in that direction, |
Constants
Public constructors
<init>
Page(
data: List<Value>,
prevKey: Key?,
nextKey: Key?)
Success result object for PagingSource.load.
Parameters | |
---|---|
data: List<Value> | Loaded data |
prevKey: Key? | Key for previous page if more data can be loaded in that direction,
null otherwise. |
nextKey: Key? | Key for next page if more data can be loaded in that direction,
null otherwise. |
<init>
Page(
data: List<Value>,
prevKey: Key?,
nextKey: Key?,
@IntRange(-2147483648) itemsBefore: Int = COUNT_UNDEFINED,
@IntRange(-2147483648) itemsAfter: Int = COUNT_UNDEFINED)
Success result object for PagingSource.load.
// One common method of pagination is to use next (and optionally previous) tokens. // The below code shows you how to data class NetworkResponseObject( val items: List<Item>, val next: String, val approximateItemsRemaining: Int ) // The following shows how you use convert such a response loaded in PagingSource.load() to // a Page, which can be returned from that method fun NetworkResponseObject.toPage() = LoadResult.Page( data = items, prevKey = null, // this implementation can only append, can't load a prepend nextKey = next, // next token will be the params.key of a subsequent append load itemsAfter = approximateItemsRemaining )
// If you load by page number, the response may not define how to load the next page. data class NetworkResponseObject( val items: List<Item> ) // The following shows how you use the current page number (e.g., the current key in // PagingSource.load() to convert a response into a Page, which can be returned from that method fun NetworkResponseObject.toPage(pageNumber: Int): LoadResult.Page<Int, Item> { return LoadResult.Page( data = items, // Since 0 is the lowest page number, return null to signify no more pages // should be loaded before it. prevKey = if (pageNumber > 0) pageNumber - 1 else null, // This API defines that it's out of data when a page returns empty. When out of // data, we return `null` to signify no more pages should be loaded // If the response instead nextKey = if (items.isNotEmpty()) pageNumber + 1 else null ) }
Properties
nextKey
val nextKey: Key?
Key for next page if more data can be loaded in that direction, null
otherwise.
prevKey
val prevKey: Key?
Key for previous page if more data can be loaded in that direction, null
otherwise.