# 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)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  class PagingState<Key : Any, Value : Any>
  ```

*** ** * ** ***

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

## Summary

| ### Public constructors |
|---|---|
| `<Key : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html, Value : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/PagingState#PagingState(kotlin.collections.List,kotlin.Int,androidx.paging.PagingConfig,kotlin.Int)( pages: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page<Key, Value>>, anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html?, config: https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig, leadingPlaceholderCount: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` | Cmn |

| ### Public functions |
|---|---|---|
| `Value?` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)( anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, predicate: (value) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Coerces `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)` to closest loaded value in `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#pages()` that matches `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#closestItemAroundPosition(kotlin.Int,kotlin.Function1)`. | Cmn |
| `Value?` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)(anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Coerces `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#closestItemToPosition(kotlin.Int)` to closest loaded value in `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#pages()`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page<Key, Value>?` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#closestPageToPosition(kotlin.Int)(anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Coerces an index in the list, including placeholders, to closest loaded page in `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#pages()`. | Cmn |
| `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#equals(kotlin.Any)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` | Cmn |
| `Value?` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#firstItemOrNull()()` | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#hashCode()()` | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#isEmpty()()` | Cmn |
| `Value?` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#lastItemOrNull()()` | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#toString()()` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html?` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#anchorPosition()` Most recently accessed index in the list, including placeholders. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#config()` `https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig` that was given when initializing the `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` stream. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page<Key, Value>>` | `https://developer.android.com/reference/kotlin/androidx/paging/PagingState#pages()` Loaded pages of data in the list. | Cmn |

## Public constructors

### PagingState

Cmn

```
<Key : Any, Value : Any> PagingState(
    pages: List<PagingSource.LoadResult.Page<Key, Value>>,
    anchorPosition: Int?,
    config: PagingConfig,
    leadingPlaceholderCount: @IntRange(from = 0) Int
)
```

## Public functions

### closestItemAroundPosition

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

```
fun closestItemAroundPosition(
    anchorPosition: Int,
    predicate: (value) -> Boolean
): Value?
```

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

This function can be called with `https://developer.android.com/reference/kotlin/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/kotlin/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 |
|---|---|
| `anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Index in the list, including placeholders. |
| `predicate: (value) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | the predicate that matches target item |

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

### closestItemToPosition

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

```
fun closestItemToPosition(anchorPosition: Int): Value?
```

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

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

| Parameters |
|---|---|
| `anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Index in the list, including placeholders. |

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

### closestPageToPosition

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

```
fun closestPageToPosition(anchorPosition: Int): PagingSource.LoadResult.Page<Key, Value>?
```

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

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

| Parameters |
|---|---|
| `anchorPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Index in the list, including placeholders. |

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

### equals

Cmn

```
open operator fun equals(other: Any?): Boolean
```

### firstItemOrNull

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

```
fun firstItemOrNull(): Value?
```

| 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/kotlin/androidx/paging/PagingState` was created. |

### hashCode

Cmn

```
open fun hashCode(): Int
```

### isEmpty

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

```
fun isEmpty(): Boolean
```

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

### lastItemOrNull

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

```
fun lastItemOrNull(): Value?
```

| 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/kotlin/androidx/paging/PagingState` was created. |

### toString

Cmn

```
open fun toString(): String
```

## Public properties

### anchorPosition

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

```
val anchorPosition: Int?
```

Most recently accessed index in the list, including placeholders.

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

### config

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

```
val config: PagingConfig
```

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

### pages

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

```
val pages: List<PagingSource.LoadResult.Page<Key, Value>>
```

Loaded pages of data in the list.