AsyncListUtil
open class AsyncListUtil<T : Any!>
kotlin.Any | |
↳ | androidx.recyclerview.widget.AsyncListUtil |
A utility class that supports asynchronous content loading.
It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while keeping UI and cache synchronous for better user experience.
It loads the data on a background thread and keeps only a limited number of fixed sized chunks in memory at all times.
AsyncListUtil
queries the currently visible range through ViewCallback
, loads the required data items in the background through DataCallback
, and notifies a ViewCallback
when the data is loaded. It may load some extra items for smoother scrolling.
Note that this class uses a single thread to load the data, so it suitable to load data from secondary storage such as disk, but not from network.
This class is designed to work with RecyclerView
, but it does not depend on it and can be used with other list views.
Summary
Nested classes |
|
---|---|
abstract |
The callback that provides data access for |
abstract |
The callback that links |
Public constructors |
|
---|---|
<init>(@NonNull klass: Class<T>, tileSize: Int, @NonNull dataCallback: AsyncListUtil.DataCallback<T>, @NonNull viewCallback: AsyncListUtil.ViewCallback) Creates an AsyncListUtil. |
Public methods |
|
---|---|
open T? |
Returns the data item at the given position or |
open Int |
Returns the number of items in the data set. |
open Unit |
Updates the currently visible item range. |
open Unit |
refresh() Forces reloading the data. |
Public constructors
<init>
AsyncListUtil(@NonNull klass: Class<T>, tileSize: Int, @NonNull dataCallback: AsyncListUtil.DataCallback<T>, @NonNull viewCallback: AsyncListUtil.ViewCallback)
Creates an AsyncListUtil.
Parameters | |
---|---|
klass |
Class<T>: Class of the data item. |
tileSize |
Class<T>: Number of item per chunk loaded at once. |
dataCallback |
Class<T>: Data access callback. |
viewCallback |
Class<T>: Callback for querying visible item range and update notifications. |
Public methods
getItem
@Nullable open fun getItem(position: Int): T?
Returns the data item at the given position or null
if it has not been loaded yet.
If this method has been called for a specific position and returned null
, then ViewCallback#onItemLoaded(int)
will be called when it finally loads. Note that if this position stays outside of the cached item range (as defined by ViewCallback#extendRangeInto
method), then the callback will never be called for this position.
Parameters | |
---|---|
position |
Int: Item position. |
Return | |
---|---|
T?: The data item at the given position or null if it has not been loaded yet. |
getItemCount
open fun getItemCount(): Int
Returns the number of items in the data set.
This is the number returned by a recent call to DataCallback#refreshData()
.
Return | |
---|---|
Int: Number of items. |
onRangeChanged
open fun onRangeChanged(): Unit
Updates the currently visible item range.
Identifies the data items that have not been loaded yet and initiates loading them in the background. Should be called from the view's scroll listener (such as RecyclerView.OnScrollListener#onScrolled
).
refresh
open fun refresh(): Unit
Forces reloading the data.
Discards all the cached data and reloads all required data items for the currently visible range. To be called when the data item count and/or contents has changed.