{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
public final class AsyncPagingDataDiffer<T extends Object>
Helper class for mapping a PagingData
into a androidx.recyclerview.widget.RecyclerView.Adapter.
For simplicity, PagingDataAdapter
can often be used in place of this class. AsyncPagingDataDiffer
is exposed for complex cases, and where overriding PagingDataAdapter
to support paging isn't convenient.
Public fields |
|
---|---|
final int |
Get the number of items currently presented by this Differ. |
final @NonNull Flow<@NonNull CombinedLoadStates> |
A hot |
Public constructors |
|
---|---|
<T extends Object> AsyncPagingDataDiffer( |
Public methods |
|
---|---|
final void |
addLoadStateListener( Add a |
final @Nullable T |
Get the item from the current PagedList at the specified index. |
final @Nullable T |
Returns the presented item at the specified position, without notifying Paging of the item access that would normally trigger page loads. |
final void |
refresh() Refresh the data presented by this |
final void |
removeLoadStateListener( Remove a previously registered |
final void |
retry() Retry any failed load requests that would result in a |
final @NonNull ItemSnapshotList<@NonNull T> |
snapshot() Returns a new |
final void |
submitData(@NonNull PagingData<@NonNull T> pagingData) Present a |
final void |
submitData( Present a |
@NonNull
public final int itemCount
Get the number of items currently presented by this Differ. This value can be directly returned to androidx.recyclerview.widget.RecyclerView.Adapter.getItemCount.
Returns | |
---|---|
int |
Number of items being presented, including placeholders. |
@NonNull
public final @NonNull Flow<@NonNull CombinedLoadStates> loadStateFlow
A hot Flow
of CombinedLoadStates
that emits a snapshot whenever the loading state of the current PagingData
changes.
This flow is conflated, so it buffers the last update to CombinedLoadStates
and immediately delivers the current load states on collection.
val adapter = UserPagingAdapter() lifecycleScope.launch { adapter.loadStateFlow .map { it.refresh } .distinctUntilChanged() .collectLatest { // show a retry button outside the list when refresh hits an error retryButton.isVisible = it is LoadState.Error // swipeRefreshLayout displays whether refresh is occurring swipeRefreshLayout.isRefreshing = it is LoadState.Loading // show an empty state over the list when loading initially, before items are loaded emptyState.isVisible = it is LoadState.Loading && adapter.itemCount == 0 } }
@NonNull
public final <T extends Object> AsyncPagingDataDiffer(
@NonNull <ERROR CLASS><@NonNull T> diffCallback,
@NonNull <ERROR CLASS> updateCallback,
@NonNull CoroutineDispatcher mainDispatcher,
@NonNull CoroutineDispatcher workerDispatcher
)
@NonNull
public final void addLoadStateListener(
@NonNull Function1<@NonNull CombinedLoadStates, Unit> listener
)
Add a CombinedLoadStates
listener to observe the loading state of the current PagingData
.
As new PagingData
generations are submitted and displayed, the listener will be notified to reflect the current CombinedLoadStates
.
val adapter = UserPagingAdapter() adapter.addLoadStateListener { // show a retry button outside the list when refresh hits an error retryButton.isVisible = it.refresh is LoadState.Error // swipeRefreshLayout displays whether refresh is occurring swipeRefreshLayout.isRefreshing = it.refresh is LoadState.Loading // show an empty state over the list when loading initially, before items are loaded emptyState.isVisible = it.refresh is LoadState.Loading && adapter.itemCount == 0 }
Parameters | |
---|---|
@NonNull Function1<@NonNull CombinedLoadStates, Unit> listener |
|
See also | |
---|---|
removeLoadStateListener |
@Nullable
public final T getItem(@IntRange int index)
Get the item from the current PagedList at the specified index.
Note that this operates on both loaded items and null padding within the PagedList.
Returns | |
---|---|
T |
The item, or |
@Nullable
public final T peek(@IntRange int index)
Returns the presented item at the specified position, without notifying Paging of the item access that would normally trigger page loads.
Parameters | |
---|---|
@IntRange int index |
Index of the presented item to return, including placeholders. |
Returns | |
---|---|
T |
The presented item at position |
@NonNull
public final void refresh()
Refresh the data presented by this AsyncPagingDataDiffer
.
refresh
triggers the creation of a new PagingData
with a new instance of PagingSource
to represent an updated snapshot of the backing dataset. If a RemoteMediator
is set, calling refresh
will also trigger a call to RemoteMediator.load
with LoadType
to allow RemoteMediator
to check for updates to the dataset backing PagingSource
.
Note: This API is intended for UI-driven refresh signals, such as swipe-to-refresh. Invalidation due repository-layer signals, such as DB-updates, should instead use PagingSource.invalidate
.
class MyActivity : AppCompatActivity() { private lateinit var binding: MyActivityBinding private val pagingAdapter = UserPagingAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = MyActivityBinding.inflate(layoutInflater) setContentView(binding.root) binding.recyclerView.adapter = pagingAdapter pagingAdapter.addLoadStateListener { loadStates -> binding.swipeRefreshLayout.isRefreshing = loadStates.refresh is LoadState.Loading } binding.swipeRefreshLayout.setOnRefreshListener { pagingAdapter.refresh() } } }
See also | |
---|---|
invalidate |
@NonNull
public final void removeLoadStateListener(
@NonNull Function1<@NonNull CombinedLoadStates, Unit> listener
)
Remove a previously registered CombinedLoadStates
listener.
Parameters | |
---|---|
@NonNull Function1<@NonNull CombinedLoadStates, Unit> listener |
Previously registered listener. |
See also | |
---|---|
addLoadStateListener |
@NonNull
public final void retry()
Retry any failed load requests that would result in a LoadState.Error
update to this AsyncPagingDataDiffer
.
Unlike refresh
, this does not invalidate PagingSource
, it only retries failed loads within the same generation of PagingData
.
LoadState.Error
can be generated from two types of load requests:
@NonNull
public final ItemSnapshotList<@NonNull T> snapshot()
Returns a new ItemSnapshotList
representing the currently presented items, including any placeholders if they are enabled.
@NonNull
public final void submitData(@NonNull PagingData<@NonNull T> pagingData)
Present a PagingData
until it is invalidated by a call to refresh
or PagingSource.invalidate
.
submitData
should be called on the same CoroutineDispatcher
where updates will be dispatched to UI, typically Dispatchers.Main
. (this is done for you if you use lifecycleScope.launch {}
).
This method is typically used when collecting from a Flow
produced by Pager
. For RxJava or LiveData support, use the non-suspending overload of submitData
, which accepts a Lifecycle.
Note: This method suspends while it is actively presenting page loads from a PagingData
, until the PagingData
is invalidated. Although cancellation will propagate to this call automatically, collecting from a Pager.flow
with the intention of presenting the most up-to-date representation of your backing dataset should typically be done using collectLatest
.
See also | |
---|---|
Pager |
@NonNull
public final void submitData(
@NonNull <ERROR CLASS> lifecycle,
@NonNull PagingData<@NonNull T> pagingData
)
Present a PagingData
until it is either invalidated or another call to submitData
is made.
This method is typically used when observing a RxJava or LiveData stream produced by Pager
. For Flow
support, use the suspending overload of submitData
, which automates cancellation via CoroutineScope
instead of relying of Lifecycle.
See also | |
---|---|
submitData |
|
Pager |