Package 

Class RecyclerView.ViewHolder


  • 
    public abstract class RecyclerView.ViewHolder
    
                        

    A ViewHolder describes an item view and metadata about its place within the RecyclerView.

    Adapter implementations should subclass ViewHolder and add fields for cachingpotentially expensive findViewById results.

    While LayoutParams belong to the LayoutManager, ViewHolders belong to the adapter. Adapters should feel free to usetheir own custom ViewHolder implementations to store data that makes binding view contentseasier. Implementations should assume that individual item views will hold strong referencesto ViewHolder objects and that RecyclerView instances may holdstrong references to extra off-screen item views for caching purposes

    • Constructor Detail

      • RecyclerView.ViewHolder

        RecyclerView.ViewHolder(View itemView)
    • Method Detail

      • getLayoutPosition

         final int getLayoutPosition()

        Returns the position of the ViewHolder in terms of the latest layout pass.

        This position is mostly used by RecyclerView components to be consistent whileRecyclerView lazily processes adapter updates.

        For performance and animation reasons, RecyclerView batches all adapter updates until thenext layout pass. This may cause mismatches between the Adapter position of the item andthe position it had in the latest layout calculations.

        LayoutManagers should always call this method while doing calculations based on itempositions. All methods in LayoutManager, State, Recycler that receive a position expect it to be the layout positionof the item.

        If LayoutManager needs to call an external method that requires the adapter position ofthe item, it can use getAbsoluteAdapterPosition or convertPreLayoutPositionToPostLayout.

      • getBindingAdapterPosition

         final int getBindingAdapterPosition()

        Returns the Adapter position of the item represented by this ViewHolder with respect tothe Adapter that bound it.

        Note that this might be different than the getLayoutPosition if there arepending adapter updates but a new layout pass has not happened yet.

        RecyclerView does not handle any adapter updates until the next layout traversal. Thismay create temporary inconsistencies between what user sees on the screen and whatadapter contents have. This inconsistency is not important since it will be less than16ms but it might be a problem if you want to use ViewHolder position to access theadapter. Sometimes, you may need to get the exact adapter position to dosome actions in response to user events. In that case, you should use this method whichwill calculate the Adapter position of the ViewHolder.

        Note that if you've called notifyDataSetChanged, until thenext layout pass, the return value of this method will be NO_POSITION.

        If the Adapter that bound this ViewHolder is inside another Adapter (e.g. ConcatAdapter), this position might be different than getAbsoluteAdapterPosition. If you would like to know the position that RecyclerView considers (e.g. for saved state), you should use getAbsoluteAdapterPosition.

      • getAbsoluteAdapterPosition

         final int getAbsoluteAdapterPosition()

        Returns the Adapter position of the item represented by this ViewHolder with respect tothe RecyclerView's Adapter. If the Adapter that bound this ViewHolder is inside another adapter (e.g. ConcatAdapter), thisposition might be different and will includethe offsets caused by other adapters in the ConcatAdapter.

        Note that this might be different than the getLayoutPosition if there arepending adapter updates but a new layout pass has not happened yet.

        RecyclerView does not handle any adapter updates until the next layout traversal. Thismay create temporary inconsistencies between what user sees on the screen and whatadapter contents have. This inconsistency is not important since it will be less than16ms but it might be a problem if you want to use ViewHolder position to access theadapter. Sometimes, you may need to get the exact adapter position to dosome actions in response to user events. In that case, you should use this method whichwill calculate the Adapter position of the ViewHolder.

        Note that if you've called notifyDataSetChanged, until thenext layout pass, the return value of this method will be NO_POSITION.

        Note that if you are querying the position as RecyclerView sees, you should use getAbsoluteAdapterPosition (e.g. you want to use it to save scrollstate). If you are querying the position to access the Adapter contents,you should use getBindingAdapterPosition.

      • getOldPosition

         final int getOldPosition()

        When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHoldersto perform animations.

        If a ViewHolder was laid out in the previous onLayout call, old position will keep itsadapter index in the previous layout.

      • getItemId

         final long getItemId()

        Returns The itemId represented by this ViewHolder.

      • setIsRecyclable

         final void setIsRecyclable(boolean recyclable)

        Informs the recycler whether this item can be recycled. Views which are notrecyclable will not be reused for other items until setIsRecyclable() islater set to true. Calls to setIsRecyclable() should always be paired (onecall to setIsRecyclabe(false) should always be matched with a later call tosetIsRecyclable(true)). Pairs of calls may be nested, as the state is internallyreference-counted.

        Parameters:
        recyclable - Whether this item is available to be recycled.