ItemKeyedDataSource
public
abstract
class
ItemKeyedDataSource
extends DataSource<Key, Value>
java.lang.Object | ||
↳ | android.arch.paging.DataSource<Key, Value> | |
↳ | android.arch.paging.ItemKeyedDataSource<Key, Value> |
Incremental data loader for paging keyed content, where loaded content uses previously loaded items as input to future loads.
Implement a DataSource using ItemKeyedDataSource if you need to use data from item N - 1
to load item N
. This is common, for example, in sorted database queries where
attributes of the item such just before the next query define how to execute it.
The InMemoryByItemRepository
in the
PagingWithNetworkSample
shows how to implement a network ItemKeyedDataSource using
Retrofit, while
handling swipe-to-refresh, network errors, and retry.
Summary
Nested classes | |
---|---|
class |
ItemKeyedDataSource.LoadCallback<Value>
Callback for ItemKeyedDataSource |
class |
ItemKeyedDataSource.LoadInitialCallback<Value>
Callback for |
class |
ItemKeyedDataSource.LoadInitialParams<Key>
Holder object for inputs to |
class |
ItemKeyedDataSource.LoadParams<Key>
Holder object for inputs to |
Public constructors | |
---|---|
ItemKeyedDataSource()
|
Public methods | |
---|---|
abstract
Key
|
getKey(Value item)
Return a key associated with the given item. |
abstract
void
|
loadAfter(LoadParams<Key> params, LoadCallback<Value> callback)
Load list data after the key specified in |
abstract
void
|
loadBefore(LoadParams<Key> params, LoadCallback<Value> callback)
Load list data before the key specified in |
abstract
void
|
loadInitial(LoadInitialParams<Key> params, LoadInitialCallback<Value> callback)
Load initial data. |
final
<ToValue>
ItemKeyedDataSource<Key, ToValue>
|
map(Function<Value, ToValue> function)
Applies the given function to each value emitted by the DataSource. |
final
<ToValue>
ItemKeyedDataSource<Key, ToValue>
|
mapByPage(Function<List<Value>, List<ToValue>> function)
Applies the given function to each value emitted by the DataSource. |
Inherited methods | |
---|---|
Public constructors
ItemKeyedDataSource
ItemKeyedDataSource ()
Public methods
getKey
Key getKey (Value item)
Return a key associated with the given item.
If your ItemKeyedDataSource is loading from a source that is sorted and loaded by a unique
integer ID, you would return item.getID()
here. This key can then be passed to
loadBefore(LoadParams, LoadCallback)
or
loadAfter(LoadParams, LoadCallback)
to load additional items adjacent to the item
passed to this function.
If your key is more complex, such as when you're sorting by name, then resolving collisions
with integer ID, you'll need to return both. In such a case you would use a wrapper class,
such as Pair<String, Integer>
or, in Kotlin,
data class Key(val name: String, val id: Int)
Parameters | |
---|---|
item |
Value : Item to get the key from. |
Returns | |
---|---|
Key |
Key associated with given item. |
loadAfter
void loadAfter (LoadParams<Key> params, LoadCallback<Value> callback)
Load list data after the key specified in LoadParams.key
.
It's valid to return a different list size than the page size if it's easier, e.g. if your backend defines page sizes. It is generally safer to increase the number loaded than reduce.
Data may be passed synchronously during the loadAfter method, or deferred and called at a later time. Further loads going down will be blocked until the callback is called.
If data cannot be loaded (for example, if the request is invalid, or the data would be stale
and inconsistent, it is valid to call invalidate()
to invalidate the data source,
and prevent further loading.
Parameters | |
---|---|
params |
LoadParams : Parameters for the load, including the key to load after, and requested size. |
callback |
LoadCallback : Callback that receives loaded data.
|
loadBefore
void loadBefore (LoadParams<Key> params, LoadCallback<Value> callback)
Load list data before the key specified in LoadParams.key
.
It's valid to return a different list size than the page size if it's easier, e.g. if your backend defines page sizes. It is generally safer to increase the number loaded than reduce.
Note: Data returned will be prepended just before the key passed, so if you vary size, ensure that the last item is adjacent to the passed key.
Data may be passed synchronously during the loadBefore method, or deferred and called at a later time. Further loads going up will be blocked until the callback is called.
If data cannot be loaded (for example, if the request is invalid, or the data would be stale
and inconsistent, it is valid to call invalidate()
to invalidate the data source,
and prevent further loading.
Parameters | |
---|---|
params |
LoadParams : Parameters for the load, including the key to load before, and requested size. |
callback |
LoadCallback : Callback that receives loaded data.
|
loadInitial
void loadInitial (LoadInitialParams<Key> params, LoadInitialCallback<Value> callback)
Load initial data.
This method is called first to initialize a PagedList with data. If it's possible to count
the items that can be loaded by the DataSource, it's recommended to pass the loaded data to
the callback via the three-parameter
onResult(List, int, int)
. This enables PagedLists
presenting data from this source to display placeholders to represent unloaded items.
requestedInitialKey
and requestedLoadSize
are hints, not requirements, so they may be altered or ignored. Note that ignoring the
requestedInitialKey
can prevent subsequent PagedList/DataSource pairs from
initializing at the same location. If your data source never invalidates (for example,
loading from the network without the network ever signalling that old data must be reloaded),
it's fine to ignore the initialLoadKey
and always start from the beginning of the
data set.
Parameters | |
---|---|
params |
LoadInitialParams : Parameters for initial load, including initial key and requested size. |
callback |
LoadInitialCallback : Callback that receives initial load data.
|
map
ItemKeyedDataSource<Key, ToValue> map (Function<Value, ToValue> function)
Applies the given function to each value emitted by the DataSource.
Same as mapByPage(Function)
, but operates on individual items.
Parameters | |
---|---|
function |
Function : Function that runs on each loaded item, returning items of a potentially
new type. |
Returns | |
---|---|
ItemKeyedDataSource<Key, ToValue> |
A new DataSource, which transforms items using the given function. |
mapByPage
ItemKeyedDataSource<Key, ToValue> mapByPage (Function<List<Value>, List<ToValue>> function)
Applies the given function to each value emitted by the DataSource.
Same as map(Function)
, but allows for batch conversions.
Parameters | |
---|---|
function |
Function : Function that runs on each loaded page, returning items of a potentially
new type. |
Returns | |
---|---|
ItemKeyedDataSource<Key, ToValue> |
A new DataSource, which transforms items using the given function. |
Interfaces
Classes
- AsyncPagedListDiffer
- DataSource
- DataSource.Factory
- ItemKeyedDataSource
- ItemKeyedDataSource.LoadCallback
- ItemKeyedDataSource.LoadInitialCallback
- ItemKeyedDataSource.LoadInitialParams
- ItemKeyedDataSource.LoadParams
- LivePagedListBuilder
- PagedList
- PagedList.BoundaryCallback
- PagedList.Builder
- PagedList.Callback
- PagedList.Config
- PagedList.Config.Builder
- PagedListAdapter
- PageKeyedDataSource
- PageKeyedDataSource.LoadCallback
- PageKeyedDataSource.LoadInitialCallback
- PageKeyedDataSource.LoadInitialParams
- PageKeyedDataSource.LoadParams
- PositionalDataSource
- PositionalDataSource.LoadInitialCallback
- PositionalDataSource.LoadInitialParams
- PositionalDataSource.LoadRangeCallback
- PositionalDataSource.LoadRangeParams
- RxPagedListBuilder
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.