# AsyncListUtil

Artifact: [androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview) [View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/AsyncListUtil.java+class:androidx.recyclerview.widget.AsyncListUtil) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
public class AsyncListUtil<T>
```

<br />

*** ** * ** ***

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.

`https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil` queries the currently visible range through `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.ViewCallback`, loads the required data items in the background through `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.DataCallback`, and notifies a `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView`, but it does not depend on it and can be used with other list views.

## Summary

| ### Nested types |
|---|
| `public abstract class https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.DataCallback<T>` The callback that provides data access for `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil`. |
| `public abstract class https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.ViewCallback` The callback that links `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil` with the list view. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil#AsyncListUtil(java.lang.Class<T>,int,androidx.recyclerview.widget.AsyncListUtil.DataCallback<T>,androidx.recyclerview.widget.AsyncListUtil.ViewCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<T> klass, int tileSize, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.DataCallback<T> dataCallback, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.ViewCallback viewCallback )` Creates an AsyncListUtil. |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable T` | `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil#getItem(int)(int position)` Returns the data item at the given position or `null` if it has not been loaded yet. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil#getItemCount()()` Returns the number of items in the data set. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil#onRangeChanged()()` Updates the currently visible item range. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil#refresh()()` Forces reloading the data. |

## Public constructors

### AsyncListUtil

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public AsyncListUtil(
    @NonNull Class<T> klass,
    int tileSize,
    @NonNull AsyncListUtil.DataCallback<T> dataCallback,
    @NonNull AsyncListUtil.ViewCallback viewCallback
)
```

Creates an AsyncListUtil.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<T> klass` | Class of the data item. |
| `int tileSize` | Number of item per chunk loaded at once. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.DataCallback<T> dataCallback` | Data access callback. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.ViewCallback viewCallback` | Callback for querying visible item range and update notifications. |

## Public methods

### getItem

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public @Nullable T getItem(int position)
```

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 `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.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 `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.ViewCallback#extendRangeInto(int[],int[],int)` method), then the callback will never be called for this position.

| Parameters |
|---|---|
| `int position` | Item position. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable T` | The data item at the given position or `null` if it has not been loaded yet. |

### getItemCount

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public int getItemCount()
```

Returns the number of items in the data set.

This is the number returned by a recent call to `https://developer.android.com/reference/androidx/recyclerview/widget/AsyncListUtil.DataCallback#refreshData()`.

| Returns |
|---|---|
| `int` | Number of items. |

### onRangeChanged

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void onRangeChanged()
```

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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.OnScrollListener#onScrolled(androidx.recyclerview.widget.RecyclerView,int,int)`).

### refresh

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void refresh()
```

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.