# RecyclerView.Recycler

# RecyclerView.Recycler

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Recycler "View this page in Java")

<br />

```
inner class RecyclerView.Recycler
```

<br />

*** ** * ** ***

A Recycler is responsible for managing scrapped or detached item views for reuse.

A "scrapped" view is a view that is still attached to its parent RecyclerView but that has been marked for removal or reuse.

Typical use of a Recycler by a[LayoutManager](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager)will be to obtain views for an adapter's data set representing the data at a given position or item ID. If the view to be reused is considered "dirty" the adapter will be asked to rebind it. If not, the view can be quickly reused by the LayoutManager with no further work. Clean views that have not[requested layout](https://developer.android.com/reference/android/view/View.html#isLayoutRequested())may be repositioned by a LayoutManager without remeasurement.

## Summary

|                                                   ### Public constructors                                                    |
|------------------------------------------------------------------------------------------------------------------------------|
| [Recycler](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#Recycler())`()` |

|                                                                                                                                                           ### Public functions                                                                                                                                                           |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                                                                            | [bindViewToPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#bindViewToPosition(android.view.View,int))`(view: `[View](https://developer.android.com/reference/android/view/View.html)`, position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Binds the given View to the position.                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                                                                            | [clear](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#clear())`()` Clear scrap views out of this recycler.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                                                                                                                                                                                              | [convertPreLayoutPositionToPostLayout](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#convertPreLayoutPositionToPostLayout(int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` RecyclerView provides artificial position range (item count) in pre-layout state and automatically maps these positions to[Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter)positions when[getViewForPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getViewForPosition(int))or[bindViewToPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#bindViewToPosition(android.view.View,int))is called. |
| `(`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[RecyclerView.ViewHolder](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder)`!>` | [getScrapList](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getScrapList())`()` Returns an unmodifiable list of ViewHolders that are currently in the scrap list.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| [View](https://developer.android.com/reference/android/view/View.html)                                                                                                                                                                                                                                                                   | [getViewForPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getViewForPosition(int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Obtain a view initialized for the given position.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                                                                            | [recycleView](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#recycleView(android.view.View))`(view: `[View](https://developer.android.com/reference/android/view/View.html)`)` Recycle a detached view.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                                                                            | [setViewCacheSize](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#setViewCacheSize(int))`(viewCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Set the maximum number of detached, valid views we should retain for later use.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |

## Public constructors

### Recycler

Added in 1.0.0  

```
Recycler()
```  

## Public functions

### bindViewToPosition

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

```
fun bindViewToPosition(view: View, position: Int): Unit
```

Binds the given View to the position. The View can be a View previously retrieved via[getViewForPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getViewForPosition(int))or created by[onCreateViewHolder](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onCreateViewHolder(android.view.ViewGroup,int)).

Generally, a LayoutManager should acquire its views via[getViewForPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getViewForPosition(int))and let the RecyclerView handle caching. This is a helper method for LayoutManager who wants to handle its own recycling logic.

Note that,[getViewForPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getViewForPosition(int))already binds the View to the position so you don't need to call this method unless you want to bind this View to another position.  

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|------------------------------------------------|
| `view: `[View](https://developer.android.com/reference/android/view/View.html)          | The view to update.                            |
| `position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item to bind to this View. |

### clear

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

```
fun clear(): Unit
```

Clear scrap views out of this recycler. Detached views contained within a recycled view pool will remain.  

### convertPreLayoutPositionToPostLayout

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

```
fun convertPreLayoutPositionToPostLayout(position: Int): Int
```

RecyclerView provides artificial position range (item count) in pre-layout state and automatically maps these positions to[Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter)positions when[getViewForPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#getViewForPosition(int))or[bindViewToPosition](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Recycler#bindViewToPosition(android.view.View,int))is called.

Usually, LayoutManager does not need to worry about this. However, in some cases, your LayoutManager may need to call some custom component with item positions in which case you need the actual adapter position instead of the pre layout position. You can use this method to convert a pre-layout position to adapter (post layout) position.

Note that if the provided position belongs to a deleted ViewHolder, this method will return -1.

Calling this method in post-layout state returns the same value back.  

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The pre-layout position to convert. Must be greater or equal to 0 and less than[getItemCount](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.State#getItemCount()). |

### getScrapList

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

```
fun getScrapList(): (Mutable)List<RecyclerView.ViewHolder!>
```

Returns an unmodifiable list of ViewHolders that are currently in the scrap list.  

|                                                                                                                                                                 Returns                                                                                                                                                                  |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| `(`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[RecyclerView.ViewHolder](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder)`!>` | List of ViewHolders in the scrap list. |

### getViewForPosition

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

```
fun getViewForPosition(position: Int): View
```

Obtain a view initialized for the given position. This method should be used by[LayoutManager](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager)implementations to obtain views to represent data from an[Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter).

The Recycler may reuse a scrap or detached view from a shared pool if one is available for the correct view type. If the adapter has not indicated that the data at the given position has changed, the Recycler will attempt to hand back a scrap view that was previously initialized for that data without rebinding.  

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|-------------------------------|
| `position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position to obtain a view for |

|                                Returns                                 |
|------------------------------------------------------------------------|--------------------------------------------------------|
| [View](https://developer.android.com/reference/android/view/View.html) | A view representing the data at`position`from`adapter` |

### recycleView

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

```
fun recycleView(view: View): Unit
```

Recycle a detached view. The specified view will be added to a pool of views for later rebinding and reuse.

A view must be fully detached (removed from parent) before it may be recycled. If the View is scrapped, it will be removed from scrap list.  

|                                   Parameters                                   |
|--------------------------------------------------------------------------------|----------------------------|
| `view: `[View](https://developer.android.com/reference/android/view/View.html) | Removed view for recycling |

|                                                                                                         See also                                                                                                          |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [removeAndRecycleView](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager#removeAndRecycleView(android.view.View,androidx.recyclerview.widget.RecyclerView.Recycler)) |   |

### setViewCacheSize

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

```
fun setViewCacheSize(viewCount: Int): Unit
```

Set the maximum number of detached, valid views we should retain for later use.  

|                                        Parameters                                        |
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------|
| `viewCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Number of views to keep before sending views to the shared pool |