Register now for Android Dev Summit 2019!

ViewCacheExtension

abstract class ViewCacheExtension
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.ViewCacheExtension

ViewCacheExtension is a helper class to provide an additional layer of view caching that can be controlled by the developer.

When Recycler#getViewForPosition(int) is called, Recycler checks attached scrap and first level cache to find a matching View. If it cannot find a suitable View, Recycler will call the getViewForPositionAndType(Recycler, int, int) before checking RecycledViewPool.

Note that, Recycler never sends Views to this method to be cached. It is developers responsibility to decide whether they want to keep their Views in this custom cache or let the default recycling policy handle it.

Summary

Public constructors

ViewCacheExtension is a helper class to provide an additional layer of view caching that can be controlled by the developer.

Public methods

abstract View?
getViewForPositionAndType(@NonNull recycler: RecyclerView.Recycler, position: Int, type: Int)

Returns a View that can be binded to the given Adapter position.

Public constructors

<init>

ViewCacheExtension()

ViewCacheExtension is a helper class to provide an additional layer of view caching that can be controlled by the developer.

When Recycler#getViewForPosition(int) is called, Recycler checks attached scrap and first level cache to find a matching View. If it cannot find a suitable View, Recycler will call the getViewForPositionAndType(Recycler, int, int) before checking RecycledViewPool.

Note that, Recycler never sends Views to this method to be cached. It is developers responsibility to decide whether they want to keep their Views in this custom cache or let the default recycling policy handle it.

Public methods

getViewForPositionAndType

@Nullable abstract fun getViewForPositionAndType(@NonNull recycler: RecyclerView.Recycler, position: Int, type: Int): View?

Returns a View that can be binded to the given Adapter position.

This method should not create a new View. Instead, it is expected to return an already created View that can be re-used for the given type and position. If the View is marked as ignored, it should first call LayoutManager#stopIgnoringView(View) before returning the View.

RecyclerView will re-bind the returned View to the position if necessary.

Parameters
recycler RecyclerView.Recycler: The Recycler that can be used to bind the View
position RecyclerView.Recycler: The adapter position
type RecyclerView.Recycler: The type of the View, defined by adapter
Return
View?: A View that is bound to the given position or NULL if there is no View to re-use