{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
Contains assorted useful stuff for paging things.
You'll enjoy DataSource
a bunch.
Text after this heading is also part of documentation for androidx.paging
AsyncPagedListDiffer.PagedListListener |
This interface is deprecated. PagedList is deprecated and has been replaced by PagingData |
DataSource.InvalidatedCallback |
Invalidation callback for |
AdjacentItems |
Represents a pair of adjacent items, null values are used to signal boundary conditions. |
AsyncPagedListDiffer |
This class is deprecated. AsyncPagedListDiffer is deprecated and has been replaced by AsyncPagingDataDiffer |
AsyncPagingDataDiffer |
Helper class for mapping a |
CombinedLoadStates |
Collection of pagination |
DataSource |
Base class for loading pages of snapshot data into a |
DataSource.Factory |
Factory for DataSources. |
InvalidatingPagingSourceFactory |
Wrapper class for |
ItemKeyedDataSource |
This class is deprecated. ItemKeyedDataSource is deprecated and has been replaced by PagingSource |
ItemKeyedDataSource.LoadCallback |
Callback for ItemKeyedDataSource |
ItemKeyedDataSource.LoadInitialCallback |
Callback for |
ItemKeyedDataSource.LoadInitialParams |
Holder object for inputs to |
ItemKeyedDataSource.LoadParams |
Holder object for inputs to |
ItemSnapshotList |
Snapshot of data being presented by a androidx.pagingAsyncPagingDataDiffer or a |
ListenableFuturePagingSource |
ListenableFuture-based compatibility wrapper around |
ListenableFutureRemoteMediator |
ListenableFuture-based compatibility wrapper around |
LivePagedListBuilder |
This class is deprecated. PagedList is deprecated and has been replaced by PagingData |
LoadState |
LoadState of a PagedList load - associated with a |
LoadState.Error |
Loading hit an error. |
LoadState.NotLoading |
Indicates the |
LoadStateAdapter |
Adapter for displaying a RecyclerView item based on |
LoadStates |
Collection of pagination |
PageKeyedDataSource |
This class is deprecated. PageKeyedDataSource is deprecated and has been replaced by PagingSource |
PageKeyedDataSource.LoadCallback |
Callback for |
PageKeyedDataSource.LoadInitialCallback |
Callback for |
PageKeyedDataSource.LoadInitialParams |
Holder object for inputs to |
PageKeyedDataSource.LoadParams |
Holder object for inputs to |
PagedList |
This class is deprecated. PagedList is deprecated and has been replaced by PagingData |
PagedList.BoundaryCallback |
Signals when a PagedList has reached the end of available data. |
PagedList.Builder |
This class is deprecated. PagedList is deprecated and has been replaced by PagingData, which no longer supports constructing snapshots of loaded data manually. |
PagedList.Callback |
Callback signaling when content is loaded into the list. |
PagedList.Config |
Configures how a |
PagedList.Config.Builder |
Builder class for |
PagedListAdapter |
This class is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter |
Pager |
Primary entry point into Paging; constructor for a reactive stream of |
PagingConfig |
An object used to configure loading behavior within a |
PagingData |
Container for Paged data from a single generation of loads. |
PagingDataAdapter |
RecyclerView.Adapter base class for presenting paged data from |
PagingSource |
Base class for an abstraction of pageable static data from some source, where loading pages of data is typically an expensive operation. |
PagingSource.LoadParams |
Params for a load request on a |
PagingSource.LoadParams.Append |
Params to load a page of data from a |
PagingSource.LoadParams.Prepend |
Params to load a page of data from a |
PagingSource.LoadParams.Refresh |
Params for an initial load request on a |
PagingSource.LoadResult |
Result of a load request from |
PagingSource.LoadResult.Error |
Error result object for |
PagingSource.LoadResult.Page |
Success result object for |
PagingState |
Snapshot state of Paging system including the loaded |
PositionalDataSource |
This class is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
PositionalDataSource.LoadInitialCallback |
Callback for |
PositionalDataSource.LoadInitialParams |
Holder object for inputs to |
PositionalDataSource.LoadRangeCallback |
Callback for PositionalDataSource |
PositionalDataSource.LoadRangeParams |
Holder object for inputs to |
RemoteMediator |
Defines a set of callbacks used to incrementally load data from a remote source into a local source wrapped by a |
RemoteMediator.MediatorResult |
|
RemoteMediator.MediatorResult.Error |
Recoverable error that can be retried, sets the |
RemoteMediator.MediatorResult.Success |
Success signaling that |
RxPagedListBuilder |
This class is deprecated. PagedList is deprecated and has been replaced by PagingData |
DiffingChangePayload |
Payloads used to dispatch change events. |
LoadType |
Type of load a |
RemoteMediator.InitializeAction |
Return type of |
TerminalSeparatorType |
Mode for configuring when terminal separators (header and footer) would be displayed by the |
ExperimentalPagingApi |
Marks experimental Paging APIs, which may have known issues that would likely be solved by a source-incompatible change in newer versions of the artifact that supplies it. |
PagedList.Config |
Config( Constructs a |
PagedList<Value> |
<Key : Any, Value : Any> This function is deprecated. DataSource is deprecated and has been replaced by PagingSource |
<ERROR CLASS><PagingData<Value>> |
A LiveData of |
@<ERROR CLASS> Flow<PagingData<T>> |
<T : Any> Flow<PagingData<T>>.cachedIn(scope: CoroutineScope) Caches the |
<ERROR CLASS> |
<T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(lifecycle: <ERROR CLASS>) Operator which caches a LiveData of |
<ERROR CLASS> |
<T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(viewModel: <ERROR CLASS>) Operator which caches a LiveData of |
<ERROR CLASS> |
<T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(scope: CoroutineScope) Operator which caches a LiveData of |
@<ERROR CLASS> PagingData<T> |
<T : Any> PagingData<T>.filter(predicate: suspend (T) -> Boolean) Returns a |
@<ERROR CLASS> PagingData<T> |
<T : Any> PagingData<T>.filter(executor: Executor, predicate: (T) -> Boolean) Returns a |
@<ERROR CLASS> PagingData<T> |
<T : Any> PagingData<T>.filterAsync( Returns a |
@<ERROR CLASS> PagingData<R> |
<T : Any, R : Any> PagingData<T>.flatMap(transform: suspend (T) -> Iterable<R>) Returns a |
@<ERROR CLASS> PagingData<R> |
Returns a |
@<ERROR CLASS> PagingData<R> |
<T : Any, R : Any> PagingData<T>.flatMapAsync( Returns a |
@<ERROR CLASS> PagingData<T> |
<T : Any> PagingData<T>.insertFooterItem( Returns a |
@<ERROR CLASS> PagingData<T> |
<T : Any> PagingData<T>.insertHeaderItem( Returns a |
@<ERROR CLASS> PagingData<R> |
<T : R, R : Any> PagingData<T>.insertSeparators( Returns a |
@<ERROR CLASS> PagingData<R> |
<R : Any, T : R> PagingData<T>.insertSeparators( Returns a |
@<ERROR CLASS> PagingData<R> |
<T : R, R : Any> PagingData<T>.insertSeparatorsAsync( Returns a |
@<ERROR CLASS> PagingData<R> |
<T : Any, R : Any> PagingData<T>.map(transform: suspend (T) -> R) Returns a |
@<ERROR CLASS> PagingData<R> |
<T : Any, R : Any> PagingData<T>.map(executor: Executor, transform: (T) -> R) Returns a |
@<ERROR CLASS> PagingData<R> |
<T : Any, R : Any> PagingData<T>.mapAsync( Returns a |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> DataSource.Factory<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> DataSource.Factory<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> () -> PagingSource<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> () -> PagingSource<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> DataSource.Factory<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> DataSource.Factory<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> () -> PagingSource<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> () -> PagingSource<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> DataSource.Factory<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> DataSource.Factory<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> () -> PagingSource<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
<ERROR CLASS><PagedList<Value>> |
<Key : Any, Value : Any> () -> PagingSource<Key, Value>. This function is deprecated. PagedList is deprecated and has been replaced by PagingData |
fun Config(
pageSize: Int,
prefetchDistance: Int = pageSize,
enablePlaceholders: Boolean = true,
initialLoadSizeHint: Int = pageSize * PagedList.Config.Builder.DEFAULT_INITIAL_PAGE_MULTIPLIER,
maxSize: Int = PagedList.Config.MAX_SIZE_UNBOUNDED
): PagedList.Config
Constructs a PagedList.Config
, convenience for PagedList.Config.Builder
.
Parameters | |
---|---|
pageSize: Int |
Number of items loaded at once from the |
prefetchDistance: Int = pageSize |
Distance the PagedList should prefetch. |
enablePlaceholders: Boolean = true |
False if null placeholders should be disabled. |
initialLoadSizeHint: Int = pageSize * PagedList.Config.Builder.DEFAULT_INITIAL_PAGE_MULTIPLIER |
Number of items to load while initializing the PagedList. |
maxSize: Int = PagedList.Config.MAX_SIZE_UNBOUNDED |
Maximum number of items to keep in memory, or |
fun <Key : Any, Value : Any>PagedList(
dataSource: DataSource<Key, Value>,
config: PagedList.Config,
notifyExecutor: Executor,
fetchExecutor: Executor,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
initialKey: Key? = null
): PagedList<Value>
Constructs a PagedList
, convenience for PagedList.Builder
.
Parameters | |
---|---|
<Key : Any> |
Type of key used to load data from the |
<Value : Any> |
Type of items held and loaded by the |
dataSource: DataSource<Key, Value> |
|
config: PagedList.Config |
Config that defines how the |
notifyExecutor: Executor |
|
fetchExecutor: Executor |
|
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
|
initialKey: Key? = null |
|
val Pager<Key, Value>.liveData: <ERROR CLASS><PagingData<Value>>
A LiveData of PagingData
, which mirrors the stream provided by Pager.flow
, but exposes it as a LiveData.
@<ERROR CLASS>
fun <T : Any> Flow<PagingData<T>>.cachedIn(scope: CoroutineScope): @<ERROR CLASS> Flow<PagingData<T>>
Caches the PagingData
such that any downstream collection from this flow will share the same PagingData
.
The flow is kept active as long as the given scope
is active. To avoid leaks, make sure to use a scope
that is already managed (like a ViewModel scope) or manually cancel it when you don't need paging anymore.
A common use case for this caching is to cache PagingData
in a ViewModel. This can ensure that, upon configuration change (e.g. rotation), then new Activity will receive the existing data immediately rather than fetching it from scratch.
Calling cachedIn
is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData
emitted by Pager
or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData
is an unsupported operation.
Note that this does not turn the Flow<PagingData>
into a hot stream. It won't execute any unnecessary code unless it is being collected.
import androidx.activity.viewModels import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.cachedIn import androidx.paging.map class MyViewModel : ViewModel() { val flow = Pager( config = PagingConfig(pageSize = 40), pagingSourceFactory = pagingSourceFactory ).flow // Loads and transformations before the cachedIn operation will be cached, so that // multiple observers get the same data. This is true either for simultaneous // observers, or e.g. an Activity re-subscribing after being recreated .cachedIn(viewModelScope) } class MyActivity : AppCompatActivity() { val pagingAdapter = MyPagingAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val viewModel by viewModels<MyViewModel>() lifecycleScope.launch { viewModel.flow // Any transformations after the ViewModel's cachedIn step will not be cached, // and will instead by re-run immediately on Activity re-creation. .map { pagingData -> // example un-cached transformation pagingData.map { UiModel(it) } } .collectLatest { pagingAdapter.submitData(it) } } } }
Parameters | |
---|---|
scope: CoroutineScope |
The coroutine scope where this page cache will be kept alive. |
fun <T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(lifecycle: <ERROR CLASS>): <ERROR CLASS>
Operator which caches a LiveData of PagingData
within the scope of a Lifecycle.
cachedIn
multicasts pages loaded and transformed by a PagingData
, allowing multiple observers on the same instance of PagingData
to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.
Calling cachedIn
is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData
emitted by Pager
or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData
is an unsupported operation.
Parameters | |
---|---|
lifecycle: <ERROR CLASS> |
The Lifecycle where the page cache will be kept alive. |
fun <T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(viewModel: <ERROR CLASS>): <ERROR CLASS>
Operator which caches a LiveData of PagingData
within the scope of a ViewModel.
cachedIn
multicasts pages loaded and transformed by a PagingData
, allowing multiple observers on the same instance of PagingData
to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.
Calling cachedIn
is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData
emitted by Pager
or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData
is an unsupported operation.
Parameters | |
---|---|
viewModel: <ERROR CLASS> |
The ViewModel whose viewModelScope will dictate how long the page cache will be kept alive. |
fun <T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(scope: CoroutineScope): <ERROR CLASS>
Operator which caches a LiveData of PagingData
within a CoroutineScope
.
cachedIn
multicasts pages loaded and transformed by a PagingData
, allowing multiple observers on the same instance of PagingData
to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.
Calling cachedIn
is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData
emitted by Pager
or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData
is an unsupported operation.
Parameters | |
---|---|
scope: CoroutineScope |
The |
@<ERROR CLASS>
fun <T : Any> PagingData<T>.filter(predicate: suspend (T) -> Boolean): @<ERROR CLASS> PagingData<T>
Returns a PagingData
containing only elements matching the given predicate
@<ERROR CLASS>
fun <T : Any> PagingData<T>.filter(executor: Executor, predicate: (T) -> Boolean): @<ERROR CLASS> PagingData<T>
Returns a PagingData
containing only elements matching the given predicate
.
@<ERROR CLASS>
fun <T : Any> PagingData<T>.filterAsync(
predicate: <ERROR CLASS><T, Boolean>,
executor: Executor
): @<ERROR CLASS> PagingData<T>
Returns a PagingData
containing only elements matching the given predicate
.
Parameters | |
---|---|
predicate: <ERROR CLASS><T, Boolean> |
AsyncFunction returning |
executor: Executor |
|
@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.flatMap(transform: suspend (T) -> Iterable<R>): @<ERROR CLASS> PagingData<R>
Returns a PagingData
of all elements returned from applying the given transform
to each element, as it is loaded.
@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.flatMap(
executor: Executor,
transform: (T) -> Iterable<R>
): @<ERROR CLASS> PagingData<R>
Returns a PagingData
of all elements returned from applying the given transform
to each element, as it is loaded.
@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.flatMapAsync(
transform: <ERROR CLASS><T, Iterable<R>>,
executor: Executor
): @<ERROR CLASS> PagingData<R>
Returns a PagingData
of all elements returned from applying the given transform
to each element, as it is loaded.
Parameters | |
---|---|
transform: <ERROR CLASS><T, Iterable<R>> |
AsyncFunction to transform an item of type |
executor: Executor |
|
@<ERROR CLASS>
fun <T : Any> PagingData<T>.insertFooterItem(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
item: T
): @<ERROR CLASS> PagingData<T>
Returns a PagingData
containing each original element, with the passed footer item
added to the end of the list.
The footer item
is added to a loaded page which marks the end of the data stream in the LoadType.APPEND
direction, either by returning null in PagingSource.LoadResult.Page.nextKey
. It will be removed if the last page in the list is dropped, which can happen in the case of loaded pages exceeding PagedList.Config.maxSize
.
Note: This operation is not idempotent, calling it multiple times will continually add more footers to the end of the list, which can be useful if multiple footer items are required.
Parameters | |
---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
item: T |
The footer to add to the end of the list once it is fully loaded in the |
See also | |
---|---|
insertHeaderItem |
@<ERROR CLASS>
fun <T : Any> PagingData<T>.insertHeaderItem(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
item: T
): @<ERROR CLASS> PagingData<T>
Returns a PagingData
containing each original element, with the passed header item
added to the start of the list.
The header item
is added to a loaded page which marks the end of the data stream in the LoadType.PREPEND
direction by returning null in PagingSource.LoadResult.Page.prevKey
. It will be removed if the first page in the list is dropped, which can happen in the case of loaded pages exceeding PagedList.Config.maxSize
.
Note: This operation is not idempotent, calling it multiple times will continually add more headers to the start of the list, which can be useful if multiple header items are required.
Parameters | |
---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
item: T |
The header to add to the front of the list once it is fully loaded in the |
See also | |
---|---|
insertFooterItem |
@<ERROR CLASS>
fun <T : R, R : Any> PagingData<T>.insertSeparators(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
generator: suspend (T?, T?) -> R?
): @<ERROR CLASS> PagingData<R>
Returns a PagingData
containing each original element, with an optional separator generated by generator
, given the elements before and after (or null, in boundary conditions).
Note that this transform is applied asynchronously, as pages are loaded. Potential separators between pages are only computed once both pages are loaded.
import androidx.paging.insertSeparators /* * Create letter separators in an alphabetically sorted list. * * For example, if the input is: * "apple", "apricot", "banana", "carrot" * * The operator would output: * "A", "apple", "apricot", "B", "banana", "C", "carrot" */ pagingDataStream.map { pagingData -> // map outer stream, so we can perform transformations on each paging generation pagingData.insertSeparators { before: String?, after: String? -> if (after != null && before?.first() != after.first()) { // separator - after is first item that starts with its first letter after.first().toUpperCase().toString() } else { // no separator - either end of list, or first letters of before/after are the same null } } }
import androidx.paging.insertSeparators import androidx.paging.map open class UiModel data class ItemUiModel(val item: Item) : UiModel() data class SeparatorUiModel(val char: Char) : UiModel() /* * Create letter separators in an alphabetically sorted list of Items, with UiModel objects. * * For example, if the input is (each an `Item`): * "apple", "apricot", "banana", "carrot" * * The operator would output a list of UiModels corresponding to: * "A", "apple", "apricot", "B", "banana", "C", "carrot" */ pagingDataStream.map { pagingData -> // map outer stream, so we can perform transformations on each paging generation pagingData .map { item -> ItemUiModel(item) // convert items in stream to ItemUiModel } .insertSeparators<ItemUiModel, UiModel> { before: ItemUiModel?, after: ItemUiModel? -> if (after != null && before?.item?.label?.first() != after.item.label.first()) { // separator - after is first item that starts with its first letter SeparatorUiModel(after.item.label.first().toUpperCase()) } else { // no separator - either end of list, or first letters of before/after are the same null } } }
Parameters | |
---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
generator: suspend (T?, T?) -> R? |
Generator function used to construct a separator item given the item before and the item after. For terminal separators (header and footer), the arguments passed to the generator, |
@<ERROR CLASS>
fun <R : Any, T : R> PagingData<T>.insertSeparators(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
executor: Executor,
generator: (T?, T?) -> R?
): @<ERROR CLASS> PagingData<R>
Returns a PagingData
containing each original element, with an optional separator generated by generator
, given the elements before and after (or null, in boundary conditions).
Note that this transform is applied asynchronously, as pages are loaded. Potential separators between pages are only computed once both pages are loaded.
Kotlin callers should instead use the suspending extension function variant of insertSeparators
/*
* Create letter separators in an alphabetically sorted list.
*
* For example, if the input is:
* "apple", "apricot", "banana", "carrot"
*
* The operator would output:
* "A", "apple", "apricot", "B", "banana", "C", "carrot"
*/
pagingDataStream.map(pagingData ->
// map outer stream, so we can perform transformations on each paging generation
PagingDataTransforms.insertSeparators(pagingData, bgExecutor,
(@Nullable String before, @Nullable String after) -> {
if (after != null && (before == null
|| before.charAt(0) != after.charAt(0))) {
// separator - after is first item that starts with its first
// letter
return Character.toString(
Character.toUpperCase(after.charAt(0)));
} else {
// no separator - either end of list, or first
// letters of items are the same
return null;
}
}));
/*
* Create letter separators in an alphabetically sorted list of Items, with UiModel
* objects.
*
* For example, if the input is (each an `Item`):
* "apple", "apricot", "banana", "carrot"
*
* The operator would output a list of UiModels corresponding to:
* "A", "apple", "apricot", "B", "banana", "C", "carrot"
*/
pagingDataStream.map(itemPagingData -> {
// map outer stream, so we can perform transformations on each paging generation
// first convert items in stream to UiModel.Item
PagingData<UiModel.ItemModel> itemModelPagingData = PagingDataTransforms.map(
itemPagingData, bgExecutor, UiModel.ItemModel::new);
// Now insert UiModel.Separators, which makes the PagingData of generic type UiModel
return PagingDataTransforms.insertSeparators(
itemModelPagingData, bgExecutor,
(@Nullable UiModel.ItemModel before, @Nullable UiModel.ItemModel after) -> {
if (after != null && (before == null
|| before.item.label.charAt(0) != after.item.label.charAt(0))) {
// separator - after is first item that starts with its first letter
return new UiModel.SeparatorModel(
Character.toUpperCase(after.item.label.charAt(0)));
} else {
// no separator - either end of list, or first
// letters of items are the same
return null;
}
});
});
public class UiModel {
static class ItemModel extends UiModel {
public Item item;
ItemModel(Item item) {
this.item = item;
}
}
static class SeparatorModel extends UiModel {
public char character;
SeparatorModel(char character) {
this.character = character;
}
}
}
Parameters | |
---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
executor: Executor |
|
generator: (T?, T?) -> R? |
Generator function used to construct a separator item given the item before and the item after. For terminal separators (header and footer), the arguments passed to the generator, |
@<ERROR CLASS>
fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
generator: <ERROR CLASS><AdjacentItems<T>, R?>,
executor: Executor
): @<ERROR CLASS> PagingData<R>
Returns a PagingData
containing each original element, with an optional separator generated by generator
, given the elements before and after (or null, in boundary conditions).
Note that this transform is applied asynchronously, as pages are loaded. Potential separators between pages are only computed once both pages are loaded.
import androidx.paging.insertSeparatorsAsync import androidx.paging.rxjava2.insertSeparatorsAsync /* * Create letter separators in an alphabetically sorted list. * * For example, if the input is: * "apple", "apricot", "banana", "carrot" * * The operator would output: * "A", "apple", "apricot", "B", "banana", "C", "carrot" */ pagingDataStream.map { pagingData -> // map outer stream, so we can perform transformations on each paging generation pagingData.insertSeparatorsAsync( AsyncFunction<AdjacentItems<String>, String?> { Futures.submit( Callable<String?> { val (before, after) = it!! if (after != null && before?.first() != after.first()) { // separator - after is first item that starts with its first letter after.first().toUpperCase().toString() } else { // no separator - either end of list, or first letters of before/after are the same null } }, executor ) }, executor ) }
import androidx.paging.insertSeparatorsAsync import androidx.paging.map import androidx.paging.rxjava2.insertSeparatorsAsync open class UiModel data class ItemUiModel(val item: Item) : UiModel() data class SeparatorUiModel(val char: Char) : UiModel() /* * Create letter separators in an alphabetically sorted list of Items, with UiModel objects. * * For example, if the input is (each an `Item`): * "apple", "apricot", "banana", "carrot" * * The operator would output a list of UiModels corresponding to: * "A", "apple", "apricot", "B", "banana", "C", "carrot" */ pagingDataStream.map { pagingData -> // map outer stream, so we can perform transformations on each paging generation pagingData .map { item -> ItemUiModel(item) // convert items in stream to ItemUiModel } .insertSeparatorsAsync( AsyncFunction<AdjacentItems<ItemUiModel>, UiModel?> { Futures.submit( Callable<UiModel> { val (before, after) = it!! if (after != null && before?.item?.label?.first() != after.item.label.first() ) { // separator - after is first item that starts with its first letter SeparatorUiModel(after.item.label.first().toUpperCase()) } else { // no separator - either end of list, or first letters of before/after are the same null } }, executor ) }, executor ) }
Parameters | |
---|---|
generator: <ERROR CLASS><AdjacentItems<T>, R?> |
AsyncFunction used to generate separator between two |
executor: Executor |
|
@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.map(transform: suspend (T) -> R): @<ERROR CLASS> PagingData<R>
Returns a PagingData
containing the result of applying the given transform
to each element, as it is loaded.
@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.map(executor: Executor, transform: (T) -> R): @<ERROR CLASS> PagingData<R>
Returns a PagingData
containing the result of applying the given transform
to each element, as it is loaded.
See also | |
---|---|
map |
@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.mapAsync(
transform: <ERROR CLASS><T, R>,
executor: Executor
): @<ERROR CLASS> PagingData<R>
Returns a PagingData
containing the result of applying the given transform
to each element, as it is loaded.
Parameters | |
---|---|
transform: <ERROR CLASS><T, R> |
AsyncFunction to transform an item of type |
executor: Executor |
|
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toFlowable(
config: PagedList.Config,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null,
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST
): <ERROR CLASS><PagedList<Value>>
Constructs a Flowable<PagedList>
, from this DataSource.Factory
, convenience for RxPagedListBuilder
.
The returned Flowable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
config: PagedList.Config |
Paging configuration. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST |
BackpressureStrategy for the Flowable to use. |
See also | |
---|---|
RxPagedListBuilder |
|
toObservable |
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toFlowable(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null,
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST
): <ERROR CLASS><PagedList<Value>>
Constructs a Flowable<PagedList>
, from this DataSource.Factory
, convenience for RxPagedListBuilder
.
The returned Flowable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
pageSize: Int |
Page size. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST |
BackpressureStrategy for the Flowable to use. |
See also | |
---|---|
RxPagedListBuilder |
|
toObservable |
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toFlowable(
config: PagedList.Config,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null,
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST
): <ERROR CLASS><PagedList<Value>>
Constructs a Flowable<PagedList>
, from this PagingSource
factory, convenience for RxPagedListBuilder
.
The returned Flowable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
config: PagedList.Config |
Paging configuration. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST |
BackpressureStrategy for the Flowable to use. |
See also | |
---|---|
RxPagedListBuilder |
|
toObservable |
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toFlowable(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null,
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST
): <ERROR CLASS><PagedList<Value>>
Constructs a Flowable<PagedList>
, from this PagingSource
factory, convenience for RxPagedListBuilder
.
The returned Flowable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
pageSize: Int |
Page size. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
backpressureStrategy: <ERROR CLASS> = BackpressureStrategy.LATEST |
BackpressureStrategy for the Flowable to use. |
See also | |
---|---|
RxPagedListBuilder |
|
toObservable |
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toLiveData(
config: PagedList.Config,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor()
): <ERROR CLASS><PagedList<Value>>
Constructs a LiveData<PagedList>
, from this DataSource.Factory
, convenience for LivePagedListBuilder
.
No work (such as loading) is done immediately, the creation of the first PagedList
is deferred until the LiveData is observed.
Parameters | |
---|---|
config: PagedList.Config |
Paging configuration. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor() |
|
See also | |
---|---|
LivePagedListBuilder |
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toLiveData(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor()
): <ERROR CLASS><PagedList<Value>>
Constructs a LiveData<PagedList>
, from this DataSource.Factory
, convenience for LivePagedListBuilder
.
No work (such as loading) is done immediately, the creation of the first PagedList
is deferred until the LiveData is observed.
Parameters | |
---|---|
pageSize: Int |
Page size. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor() |
Executor for fetching data from DataSources. |
See also | |
---|---|
LivePagedListBuilder |
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toLiveData(
config: PagedList.Config,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
coroutineScope: CoroutineScope = GlobalScope,
fetchDispatcher: CoroutineDispatcher = ArchTaskExecutor.getIOThreadExecutor() .asCoroutineDispatcher()
): <ERROR CLASS><PagedList<Value>>
Constructs a LiveData<PagedList>
, from this PagingSource factory, convenience for LivePagedListBuilder
.
No work (such as loading) is done immediately, the creation of the first PagedList
is deferred until the LiveData is observed.
Parameters | |
---|---|
config: PagedList.Config |
Paging configuration. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
coroutineScope: CoroutineScope = GlobalScope |
Set the Defaults to |
fetchDispatcher: CoroutineDispatcher = ArchTaskExecutor.getIOThreadExecutor()
.asCoroutineDispatcher() |
|
See also | |
---|---|
LivePagedListBuilder |
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toLiveData(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
coroutineScope: CoroutineScope = GlobalScope,
fetchDispatcher: CoroutineDispatcher = ArchTaskExecutor.getIOThreadExecutor() .asCoroutineDispatcher()
): <ERROR CLASS><PagedList<Value>>
Constructs a LiveData<PagedList>
, from this PagingSource factory, convenience for LivePagedListBuilder
.
No work (such as loading) is done immediately, the creation of the first PagedList
is deferred until the LiveData is observed.
Parameters | |
---|---|
pageSize: Int |
Page size. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
coroutineScope: CoroutineScope = GlobalScope |
Set the Defaults to |
fetchDispatcher: CoroutineDispatcher = ArchTaskExecutor.getIOThreadExecutor()
.asCoroutineDispatcher() |
|
See also | |
---|---|
LivePagedListBuilder |
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toObservable(
config: PagedList.Config,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null
): <ERROR CLASS><PagedList<Value>>
Constructs a Observable<PagedList>
from this DataSource.Factory
, convenience for RxPagedListBuilder
.
The returned Observable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
config: PagedList.Config |
Paging configuration. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to PagedList load state. |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
See also | |
---|---|
RxPagedListBuilder |
|
toFlowable |
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toObservable(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null
): <ERROR CLASS><PagedList<Value>>
Constructs a Observable<PagedList>
from this DataSource.Factory
, convenience for RxPagedListBuilder
.
The returned Observable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
pageSize: Int |
Size of pages to load. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
See also | |
---|---|
RxPagedListBuilder |
|
toFlowable |
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toObservable(
config: PagedList.Config,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null
): <ERROR CLASS><PagedList<Value>>
Constructs a Observable<PagedList>
from this PagingSource
factory, convenience for RxPagedListBuilder
.
The returned Observable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
config: PagedList.Config |
Paging configuration. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to PagedList load state. |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
See also | |
---|---|
RxPagedListBuilder |
|
toFlowable |
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toObservable(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: <ERROR CLASS>? = null,
notifyScheduler: <ERROR CLASS>? = null
): <ERROR CLASS><PagedList<Value>>
Constructs a Observable<PagedList>
from this PagingSource
factory, convenience for RxPagedListBuilder
.
The returned Observable will already be subscribed on the fetchScheduler
, and will perform all loading on that scheduler. It will already be observed on notifyScheduler
, and will dispatch new PagedList
s, as well as their updates to that scheduler.
Parameters | |
---|---|
pageSize: Int |
Size of pages to load. |
initialLoadKey: Key? = null |
Initial load key passed to the first |
boundaryCallback: PagedList.BoundaryCallback<Value>? = null |
The boundary callback for listening to |
fetchScheduler: <ERROR CLASS>? = null |
Scheduler used to fetch from |
notifyScheduler: <ERROR CLASS>? = null |
Scheduler that receives |
See also | |
---|---|
RxPagedListBuilder |
|
toFlowable |