FragmentStateAdapter

public abstract class FragmentStateAdapter
extends Adapter<FragmentViewHolder> implements StatefulAdapter

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<androidx.viewpager2.adapter.FragmentViewHolder>
     ↳ androidx.viewpager2.adapter.FragmentStateAdapter


Similar in behavior to FragmentStatePagerAdapter

Lifecycle within RecyclerView:

Summary

Nested classes

class FragmentStateAdapter.FragmentTransactionCallback

Callback interface for listening to fragment lifecycle changes that happen inside the adapter. 

Public constructors

FragmentStateAdapter(FragmentActivity fragmentActivity)
FragmentStateAdapter(Fragment fragment)
FragmentStateAdapter(FragmentManager fragmentManager, Lifecycle lifecycle)

Public methods

boolean containsItem(long itemId)

Default implementation works for collections that don't add, move, remove items.

abstract Fragment createFragment(int position)

Provide a new Fragment associated with the specified position.

long getItemId(int position)

Default implementation works for collections that don't add, move, remove items.

void onAttachedToRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it starts observing this Adapter.

final void onBindViewHolder(FragmentViewHolder holder, int position)

Called by RecyclerView to display the data at the specified position.

final FragmentViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.

void onDetachedFromRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it stops observing this Adapter.

final boolean onFailedToRecycleView(FragmentViewHolder holder)

Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state.

final void onViewAttachedToWindow(FragmentViewHolder holder)

Called when a view created by this adapter has been attached to a window.

final void onViewRecycled(FragmentViewHolder holder)

Called when a view created by this adapter has been recycled.

void registerFragmentTransactionCallback(FragmentStateAdapter.FragmentTransactionCallback callback)

Registers a FragmentStateAdapter.FragmentTransactionCallback to listen to fragment lifecycle changes that happen inside the adapter.

final void restoreState(Parcelable savedState)

Restores adapter state

final Parcelable saveState()

Saves adapter state

final void setHasStableIds(boolean hasStableIds)

Indicates whether each item in the data set can be represented with a unique identifier of type Long.

void unregisterFragmentTransactionCallback(FragmentStateAdapter.FragmentTransactionCallback callback)

Unregisters a FragmentStateAdapter.FragmentTransactionCallback.

Inherited methods

Public constructors

FragmentStateAdapter

public FragmentStateAdapter (FragmentActivity fragmentActivity)

Parameters
fragmentActivity FragmentActivity: if the ViewPager2 lives directly in a FragmentActivity subclass.

FragmentStateAdapter

public FragmentStateAdapter (Fragment fragment)

Parameters
fragment Fragment: if the ViewPager2 lives directly in a Fragment subclass.

FragmentStateAdapter

public FragmentStateAdapter (FragmentManager fragmentManager, 
                Lifecycle lifecycle)

Parameters
fragmentManager FragmentManager: of ViewPager2's host

lifecycle Lifecycle: of ViewPager2's host

Public methods

containsItem

public boolean containsItem (long itemId)

Default implementation works for collections that don't add, move, remove items.

When overriding, also override getItemId(int)

Parameters
itemId long

Returns
boolean

createFragment

public abstract Fragment createFragment (int position)

Provide a new Fragment associated with the specified position.

The adapter will be responsible for the Fragment lifecycle:

  • The Fragment will be used to display an item.
  • The Fragment will be destroyed when it gets too far from the viewport, and its state will be saved. When the item is close to the viewport again, a new Fragment will be requested, and a previously saved state will be used to initialize it.

Parameters
position int

Returns
Fragment

getItemId

public long getItemId (int position)

Default implementation works for collections that don't add, move, remove items.

When overriding, also override containsItem(long).

If the item is not a part of the collection, return RecyclerView.NO_ID.

Parameters
position int: Adapter position

Returns
long stable item id RecyclerView.Adapter.hasStableIds()

onAttachedToRecyclerView

public void onAttachedToRecyclerView (RecyclerView recyclerView)

Called by RecyclerView when it starts observing this Adapter.

Keep in mind that same adapter may be observed by multiple RecyclerViews.

Parameters
recyclerView RecyclerView: The RecyclerView instance which started observing this adapter.

onBindViewHolder

public final void onBindViewHolder (FragmentViewHolder holder, 
                int position)

Called by RecyclerView to display the data at the specified position. This method should update the contents of the RecyclerView.ViewHolder.itemView to reflect the item at the given position.

Note that unlike ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use RecyclerView.ViewHolder.getBindingAdapterPosition() which will have the updated adapter position. Override onBindViewHolder(ViewHolder, int, List) instead if Adapter can handle efficient partial bind.

Parameters
holder FragmentViewHolder: The ViewHolder which should be updated to represent the contents of the item at the given position in the data set.

position int: The position of the item within the adapte