# PagingState

Artifact: [androidx.paging:paging-common](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/PagingState.kt+class:androidx.paging.PagingState) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

*** ** * ** ***

<br />

```
public final class PagingState<Key extends Object, Value extends Object>
```

<br />

*** ** * ** ***

Snapshot state of Paging system including the loaded `https://developer.android.com/reference/androidx/paging/PagingState#pages()`, the last accessed `https://developer.android.com/reference/androidx/paging/PagingState#anchorPosition()`, and the `https://developer.android.com/reference/androidx/paging/PagingState#config()` used.

## Summary

| ### Public constructors |
|---|
| `<Key extends https://developer.android.com/reference/java/lang/Object.html, Value extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/paging/PagingState#PagingState(kotlin.collections.List,kotlin.Int,androidx.paging.PagingConfig,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>> pages, https://developer.android.com/reference/java/lang/Integer.html anchorPosition, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingConfig config, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int leadingPlaceholderCount )` |

| ### Public methods |
|---|---|
| `final Value` | `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)( int anchorPosition, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull value, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> predicate )` Coerces `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` to closest loaded value in `https://developer.android.com/reference/androidx/paging/PagingState#pages()` that matches `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)`. |
| `final Value` | `https://developer.android.com/reference/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)(int anchorPosition)` Coerces `https://developer.android.com/reference/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)` to closest loaded value in `https://developer.android.com/reference/androidx/paging/PagingState#pages()`. |
| `final https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | `https://developer.android.com/reference/androidx/paging/PagingState#closestPageToPosition(kotlin.Int)(int anchorPosition)` Coerces an index in the list, including placeholders, to closest loaded page in `https://developer.android.com/reference/androidx/paging/PagingState#pages()`. |
| `boolean` | `https://developer.android.com/reference/androidx/paging/PagingState#equals(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html other)` |
| `final Value` | `https://developer.android.com/reference/androidx/paging/PagingState#firstItemOrNull()()` |
| `final https://developer.android.com/reference/java/lang/Integer.html` | `https://developer.android.com/reference/androidx/paging/PagingState#getAnchorPosition()()` Most recently accessed index in the list, including placeholders. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingConfig` | `https://developer.android.com/reference/androidx/paging/PagingState#getConfig()()` `https://developer.android.com/reference/androidx/paging/PagingConfig` that was given when initializing the `https://developer.android.com/reference/androidx/paging/PagingData` stream. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>>` | `https://developer.android.com/reference/androidx/paging/PagingState#getPages()()` Loaded pages of data in the list. |
| `int` | `https://developer.android.com/reference/androidx/paging/PagingState#hashCode()()` |
| `final boolean` | `https://developer.android.com/reference/androidx/paging/PagingState#isEmpty()()` |
| `final Value` | `https://developer.android.com/reference/androidx/paging/PagingState#lastItemOrNull()()` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/paging/PagingState#toString()()` |

## Public constructors

### PagingState

```
public <Key extends Object, Value extends Object> PagingState(
    @NonNull List<@NonNull PagingSource.LoadResult.Page<@NonNull Key, @NonNull Value>> pages,
    Integer anchorPosition,
    @NonNull PagingConfig config,
    @IntRange(from = 0) int leadingPlaceholderCount
)
```

## Public methods

### closestItemAroundPosition

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

```
public final Value closestItemAroundPosition(
    int anchorPosition,
    @NonNull Function1<@NonNull value, @NonNull Boolean> predicate
)
```

Coerces `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` to closest loaded value in `https://developer.android.com/reference/androidx/paging/PagingState#pages()` that matches `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)`.

This function can be called with `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` to fetch the loaded item that is closest to the last accessed index in the list.

This function searches for the matching predicate amongst items that are positioned both before and after the `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` with this order of priority:

1. item closest to anchorPosition

2. item before anchorPosition

This means that given an anchorPosition of item10, if item8 and item11 both match the predicate, then item11 will be returned since it is closer to item10. If two matching items have the same proximity, then the item that comes first will be returned. So given an anchorPosition of item10, if item9 and item11 both match the predicate, then item9 will be returned since it comes before item11.

This method should be avoided if possible if used on Lists that do not support random access, otherwise performance will take a big hit.

| Parameters |
|---|---|
| `int anchorPosition` | Index in the list, including placeholders. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull value, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> predicate` | the predicate that matches target item |

| Returns |
|---|---|
| `Value` | The closest loaded `https://developer.android.com/reference/androidx/paging/PagingState` in `https://developer.android.com/reference/androidx/paging/PagingState#pages()` to the provided `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` that matches the `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)`. `null` if all loaded `https://developer.android.com/reference/androidx/paging/PagingState#pages()` are empty or if none of the loaded values match `https://developer.android.com/reference/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` |

### closestItemToPosition

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

```
public final Value closestItemToPosition(int anchorPosition)
```

Coerces `https://developer.android.com/reference/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)` to closest loaded value in `https://developer.android.com/reference/androidx/paging/PagingState#pages()`.

This function can be called with `https://developer.android.com/reference/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)` to fetch the loaded item that is closest to the last accessed index in the list.

| Parameters |
|---|---|
| `int anchorPosition` | Index in the list, including placeholders. |

| Returns |
|---|---|
| `Value` | The closest loaded `https://developer.android.com/reference/androidx/paging/PagingState` in `https://developer.android.com/reference/androidx/paging/PagingState#pages()` to the provided `https://developer.android.com/reference/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)`. `null` if all loaded `https://developer.android.com/reference/androidx/paging/PagingState#pages()` are empty. |

### closestPageToPosition

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

```
public final PagingSource.LoadResult.Page<@NonNull Key, @NonNull Value> closestPageToPosition(int anchorPosition)
```

Coerces an index in the list, including placeholders, to closest loaded page in `https://developer.android.com/reference/androidx/paging/PagingState#pages()`.

This function can be called with `https://developer.android.com/reference/androidx/paging/PagingState#closestPageToPosition(kotlin.Int)` to fetch the loaded page that is closest to the last accessed index in the list.

| Parameters |
|---|---|
| `int anchorPosition` | Index in the list, including placeholders. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Page<@https://developer.android.com/reference/androidx/annotation/NonNull Key, @https://developer.android.com/reference/androidx/annotation/NonNull Value>` | The closest loaded `https://developer.android.com/reference/androidx/paging/PagingState` in `https://developer.android.com/reference/androidx/paging/PagingState#pages()` to the provided `https://developer.android.com/reference/androidx/paging/PagingState#closestPageToPosition(kotlin.Int)`. `null` if all loaded `https://developer.android.com/reference/androidx/paging/PagingState#pages()` are empty. |

### equals

```
public boolean equals(Object other)
```

### firstItemOrNull

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

```
public final Value firstItemOrNull()
```

| Returns |
|---|---|
| `Value` | The first loaded item in the list or `null` if all loaded pages are empty or no pages were loaded when this `https://developer.android.com/reference/androidx/paging/PagingState` was created. |

### getAnchorPosition

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

```
public final Integer getAnchorPosition()
```

Most recently accessed index in the list, including placeholders.

`null` if no access in the `https://developer.android.com/reference/androidx/paging/PagingData` has been made yet. E.g., if this snapshot was generated before or during the first load.

### getConfig

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

```
public final @NonNull PagingConfig getConfig()
```

`https://developer.android.com/reference/androidx/paging/PagingConfig` that was given when initializing the `https://developer.android.com/reference/androidx/paging/PagingData` stream.

### getPages

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

```
public final @NonNull List<@NonNull PagingSource.LoadResult.Page<@NonNull Key, @NonNull Value>> getPages()
```

Loaded pages of data in the list.

### hashCode

```
public int hashCode()
```

### isEmpty

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

```
public final boolean isEmpty()
```

| Returns |
|---|---|
| `boolean` | `true` if all loaded pages are empty or no pages were loaded when this `https://developer.android.com/reference/androidx/paging/PagingState` was created, `false` otherwise. |

### lastItemOrNull

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

```
public final Value lastItemOrNull()
```

| Returns |
|---|---|
| `Value` | The last loaded item in the list or `null` if all loaded pages are empty or no pages were loaded when this `https://developer.android.com/reference/androidx/paging/PagingState` was created. |

### toString

```
public @NonNull String toString()
```