-
public final class 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 butthat has been marked for removal or reuse.
Typical use of a Recycler by a LayoutManager will be to obtain views foran 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.
-
-
Method Summary
Modifier and Type Method Description void
clear()
Clear scrap views out of this recycler. void
setViewCacheSize(int viewCount)
Set the maximum number of detached, valid views we should retain for later use. List<RecyclerView.ViewHolder>
getScrapList()
Returns an unmodifiable list of ViewHolders that are currently in the scrap list. void
bindViewToPosition(@NonNull() View view, int position)
Binds the given View to the position. int
convertPreLayoutPositionToPostLayout(int position)
RecyclerView provides artificial position range (item count) in pre-layout state andautomatically maps these positions to Adapter positions when getViewForPosition or bindViewToPosition is called. View
getViewForPosition(int position)
Obtain a view initialized for the given position. void
recycleView(@NonNull() View view)
Recycle a detached view. -
-
Method Detail
-
clear
void clear()
Clear scrap views out of this recycler. Detached views contained within arecycled view pool will remain.
-
setViewCacheSize
void setViewCacheSize(int viewCount)
Set the maximum number of detached, valid views we should retain for later use.
- Parameters:
viewCount
- Number of views to keep before sending views to the shared pool
-
getScrapList
@NonNull() List<RecyclerView.ViewHolder> getScrapList()
Returns an unmodifiable list of ViewHolders that are currently in the scrap list.
-
bindViewToPosition
void bindViewToPosition(@NonNull() View view, int position)
Binds the given View to the position. The View can be a View previously retrieved via getViewForPosition or created by onCreateViewHolder.
Generally, a LayoutManager should acquire its views via getViewForPosition and let the RecyclerView handle caching. This is a helper method for LayoutManager whowants to handle its own recycling logic.
Note that, getViewForPosition already binds the View to the position soyou don't need to call this method unless you want to bind this View to another position.
- Parameters:
view
- The view to update.position
- The position of the item to bind to this View.
-
convertPreLayoutPositionToPostLayout
int convertPreLayoutPositionToPostLayout(int position)
RecyclerView provides artificial position range (item count) in pre-layout state andautomatically maps these positions to Adapter positions when getViewForPosition or bindViewToPosition is called.
Usually, LayoutManager does not need to worry about this. However, in some cases, yourLayoutManager may need to call some custom component with item positions in whichcase you need the actual adapter position instead of the pre layout position. Youcan 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 willreturn -1.
Calling this method in post-layout state returns the same value back.
- Parameters:
position
- The pre-layout position to convert.
-
getViewForPosition
@NonNull() View getViewForPosition(int position)
Obtain a view initialized for the given position.This method should be used by LayoutManager implementations to obtainviews to represent data from an Adapter.
The Recycler may reuse a scrap or detached view from a shared pool if one isavailable for the correct view type. If the adapter has not indicated that thedata at the given position has changed, the Recycler will attempt to hand backa scrap view that was previously initialized for that data without rebinding.
- Parameters:
position
- Position to obtain a view for
-
recycleView
void recycleView(@NonNull() View view)
Recycle a detached view. The specified view will be added to a pool of viewsfor later rebinding and reuse.
A view must be fully detached (removed from parent) before it may be recycled. If theView is scrapped, it will be removed from scrap list.
- Parameters:
view
- Removed view for recycling
-
-
-
-