The Android Developer Challenge is back! Submit your idea before December 2.

Recycler

class Recycler
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.Recycler

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 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 may be repositioned by a LayoutManager without remeasurement.

Summary

Public constructors

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

Public methods

Unit
bindViewToPosition(@NonNull view: View, position: Int)

Binds the given View to the position.

Unit

Clear scrap views out of this recycler.

Int

RecyclerView provides artificial position range (item count) in pre-layout state and automatically maps these positions to Adapter positions when getViewForPosition(int) or bindViewToPosition(View, int) is called.

MutableList<RecyclerView.ViewHolder!>

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

View

Obtain a view initialized for the given position.

Unit
recycleView(@NonNull view: View)

Recycle a detached view.

Unit
setViewCacheSize(viewCount: Int)

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

Public constructors

<init>

Recycler()

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 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 may be repositioned by a LayoutManager without remeasurement.

Public methods

bindViewToPosition

fun bindViewToPosition(@NonNull view: View, position: Int): Unit

Binds the given View to the position. The View can be a View previously retrieved via getViewForPosition(int) or created by Adapter#onCreateViewHolder(ViewGroup, int).

Generally, a LayoutManager should acquire its views via 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(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: The view to update.
position View: The position of the item to bind to this View.

clear

fun clear(): Unit

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

convertPreLayoutPositionToPostLayout

fun convertPreLayoutPositionToPostLayout(position: Int): Int

RecyclerView provides artificial position range (item count) in pre-layout state and automatically maps these positions to Adapter positions when getViewForPosition(int) or bindViewToPosition(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: The pre-layout position to convert. Must be greater or equal to 0 and less than State#getItemCount().

getScrapList

@NonNull fun getScrapList(): MutableList<RecyclerView.ViewHolder!>

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

Return
MutableList<RecyclerView.ViewHolder!>: List of ViewHolders in the scrap list.

getViewForPosition

@NonNull fun getViewForPosition(position: Int): View

Obtain a view initialized for the given position. This method should be used by LayoutManager implementations to obtain views to represent data from an 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: Position to obtain a view for
Return
View: A view representing the data at position from adapter

recycleView

fun recycleView(@NonNull 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: Removed view for recycling

setViewCacheSize

fun setViewCacheSize(viewCount: Int): Unit

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

Parameters
viewCount Int: Number of views to keep before sending views to the shared pool