# StableIdKeyProvider

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

*** ** * ** ***


```
class StableIdKeyProvider : ItemKeyProvider<Long!>
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.recyclerview.selection.ItemKeyProvider](https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/ItemKeyProvider)\<[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)!\> ||
|   | ↳ | [androidx.recyclerview.selection.StableIdKeyProvider](https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/StableIdKeyProvider) |

*** ** * ** ***

An `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/ItemKeyProvider` that provides stable ids by way of cached `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter` stable ids. Items enter the cache as they are laid out by RecyclerView, and are removed from the cache as they are recycled.

There are trade-offs with this implementation as it necessarily auto-boxes `long` stable id values into `Long` values for use as selection keys. The core Selection API uses a parameterized key type to permit other keys (such as Strings or URIs).

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/StableIdKeyProvider#StableIdKeyProvider(androidx.recyclerview.widget.RecyclerView)(recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView)` Creates a new key provider that uses cached `long` stable ids associated with the RecyclerView items. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html?` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/StableIdKeyProvider#getKey(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/StableIdKeyProvider#getPosition(java.lang.Long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` |

### Inherited Constants

From [androidx.recyclerview.selection.ItemKeyProvider](https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/ItemKeyProvider) `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/ItemKeyProvider#SCOPE_CACHED() = 1`

Provides access to cached data based for items that were recently bound in the view.
`const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/ItemKeyProvider#SCOPE_MAPPED() = 0`

Provides access to all data, regardless of whether it is bound to a view or not.

## Public constructors

### StableIdKeyProvider

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

```
StableIdKeyProvider(recyclerView: RecyclerView)
```

Creates a new key provider that uses cached `long` stable ids associated with the RecyclerView items.

| Parameters |
|---|---|
| `recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` | the owner RecyclerView |

## Public functions

### getKey

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

```
fun getKey(position: Int): Long?
```

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html?` | The selection key at the given adapter position, or null. |

### getPosition

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

```
fun getPosition(key: Long): Int
```

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the position corresponding to the selection key, or RecyclerView.NO_POSITION. |