FragmentStateAdapter
abstract class FragmentStateAdapter : RecyclerView.Adapter, StatefulAdapter
| kotlin.Any | ||
| ↳ | androidx.recyclerview.widget.RecyclerView.Adapter | |
| ↳ | androidx.viewpager2.adapter.FragmentStateAdapter |
Similar in behavior to FragmentStatePagerAdapter
Lifecycle within RecyclerView:
RecyclerView.ViewHolderinitially an emptyFrameLayout, serves as a re-usable container for aFragmentin later stages.- onBindViewHolder we ask for a
Fragmentfor the position. If we already have the fragment, or have previously saved its state, we use those. - onAttachedToWindow we attach the
Fragmentto a container. onViewRecycledwe remove, save state, destroy theFragment.
Summary
Nested types |
|---|
abstract class FragmentStateAdapter.FragmentTransactionCallbackCallback interface for listening to fragment lifecycle changes that happen inside the adapter. |
|
Callback returned by |
Public constructors |
|---|
FragmentStateAdapter(fragmentActivity: FragmentActivity) |
FragmentStateAdapter(fragment: Fragment) |
FragmentStateAdapter( |
Public functions |
|
|---|---|
Boolean |
containsItem(itemId: Long)Default implementation works for collections that don't add, move, remove items. |
abstract Fragment |
createFragment(position: Int)Provide a new Fragment associated with the specified position. |
Long |
Default implementation works for collections that don't add, move, remove items. |
Unit |
@CallSuperCalled by RecyclerView when it starts observing this Adapter. |
Unit |
onBindViewHolder(holder: FragmentViewHolder, position: Int)Called by RecyclerView to display the data at the specified position. |
FragmentViewHolder |
onCreateViewHolder(parent: ViewGroup, viewType: Int)Called when RecyclerView needs a new |
Unit |
@CallSuperCalled by RecyclerView when it stops observing this Adapter. |
Boolean |
onFailedToRecycleView(holder: FragmentViewHolder)Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state. |
Unit |
onViewAttachedToWindow(holder: FragmentViewHolder)Called when a view created by this adapter has been attached to a window. |
Unit |
onViewRecycled(holder: FragmentViewHolder)Called when a view created by this adapter has been recycled. |
Unit |
Registers a |
Unit |
restoreState(savedState: Parcelable)Restores adapter state |
Parcelable |
Saves adapter state |
Unit |
setHasStableIds(hasStableIds: Boolean)Indicates whether each item in the data set can be represented with a unique identifier of type |
Unit |
Unregisters a |
Inherited functions |
||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
FragmentStateAdapter
FragmentStateAdapter(fragmentActivity: FragmentActivity)
| Parameters | |
|---|---|
fragmentActivity: FragmentActivity |
if the |
| See also | |
|---|---|
FragmentStateAdapter |
|
FragmentStateAdapter |
FragmentStateAdapter
FragmentStateAdapter(fragment: Fragment)
| Parameters | |
|---|---|
fragment: Fragment |
if the |
| See also | |
|---|---|
FragmentStateAdapter |
|
FragmentStateAdapter |
FragmentStateAdapter
FragmentStateAdapter(
fragmentManager: FragmentManager,
lifecycle: Lifecycle
)
| Parameters | |
|---|---|
fragmentManager: FragmentManager |
of |
lifecycle: Lifecycle |
of |
| See also | |
|---|---|
FragmentStateAdapter |
|
FragmentStateAdapter |
Public functions
containsItem
fun containsItem(itemId: Long): Boolean
Default implementation works for collections that don't add, move, remove items.
When overriding, also override getItemId
createFragment
abstract fun createFragment(position: Int): Fragment
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.
| See also | |
|---|---|
setOffscreenPageLimit |
getItemId
fun getItemId(position: Int): Long
Default implementation works for collections that don't add, move, remove items.
When overriding, also override containsItem.
If the item is not a part of the collection, return NO_ID.
| Parameters | |
|---|---|
position: Int |
Adapter position |
| Returns | |
|---|---|
Long |
stable item id |
onAttachedToRecyclerView
@CallSuper
fun onAttachedToRecyclerView(recyclerView: RecyclerView): Unit
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. |
| See also | |
|---|---|
onDetachedFromRecyclerView |
onBindViewHolder
fun onBindViewHolder(holder: FragmentViewHolder, position: Int): Unit
Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.
Note that unlike android.widget.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 getBindingAdapterPosition which will have the updated adapter position. Override onBindViewHolder 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 adapter's data set. |
onCreateViewHolder
fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FragmentViewHolder
Called when RecyclerView needs a new ViewHolder of the given type to represent an item.
This new ViewHolder should be constructed with a new View that can represent the items of the given type. You can either create a new View manually or inflate it from an XML layout file.
The new ViewHolder will be used to display items of the adapter using onBindViewHolder. Since it will be re-used to display different items in the data set, it is a good idea to cache references to sub views of the View to avoid unnecessary findViewById calls.
| Parameters | |
|---|---|
parent: ViewGroup |
The ViewGroup into which the new View will be added after it is bound to an adapter position. |
viewType: Int |
The view type of the new View. |
| Returns | |
|---|---|
FragmentViewHolder |
A new ViewHolder that holds a View of the given view type. |
| See also | |
|---|---|
getItemViewType |
|
onBindViewHolder |
onDetachedFromRecyclerView
@CallSuper
fun onDetachedFromRecyclerView(recyclerView: RecyclerView): Unit
Called by RecyclerView when it stops observing this Adapter.
| Parameters | |
|---|---|
recyclerView: RecyclerView |
The RecyclerView instance which stopped observing this adapter. |
| See also | |
|---|---|
onAttachedToRecyclerView |
onFailedToRecycleView
fun onFailedToRecycleView(holder: FragmentViewHolder): Boolean
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state. Upon receiving this callback, Adapter can clear the animation(s) that effect the View's transient state and return true so that the View can be recycled. Keep in mind that the View in question is already removed from the RecyclerView.
In some cases, it is acceptable to recycle a View although it has transient state. Most of the time, this is a case where the transient state will be cleared in onBindViewHolder call when View is rebound to a new position. For this reason, RecyclerView leaves the decision to the Adapter and uses the return value of this method to decide whether the View should be recycled or not.
Note that when all animations are created by RecyclerView.ItemAnimator, you should never receive this callback because RecyclerView keeps those Views as children until their animations are complete. This callback is useful when children of the item views create animations which may not be easy to implement using an ItemAnimator.
You should never fix this issue by calling holder.itemView.setHasTransientState(false); unless you've previously called holder.itemView.setHasTransientState(true);. Each View.setHasTransientState(true) call must be matched by a View.setHasTransientState(false) call, otherwise, the state of the View may become inconsistent. You should always prefer to end or cancel animations that are triggering the transient state instead of handling it manually.
| Parameters | |
|---|---|
holder: FragmentViewHolder |
The ViewHolder containing the View that could not be recycled due to its transient state. |
| Returns | |
|---|---|
Boolean |
True if the View should be recycled, false otherwise. Note that if this method returns |
onViewAttachedToWindow
fun onViewAttachedToWindow(holder: FragmentViewHolder): Unit
Called when a view created by this adapter has been attached to a window.
This can be used as a reasonable signal that the view is about to be seen by the user. If the adapter previously freed any resources in onViewDetachedFromWindow those resources should be restored here.
| Parameters | |
|---|---|
holder: FragmentViewHolder |
Holder of the view being attached |
onViewRecycled
fun onViewRecycled(holder: FragmentViewHolder): Unit
Called when a view created by this adapter has been recycled.
A view is recycled when a LayoutManager decides that it no longer needs to be attached to its parent RecyclerView. This can be because it has fallen out of visibility or a set of cached views represented by views still attached to the parent RecyclerView. If an item view has large or expensive data bound to it such as large bitmaps, this may be a good place to release those resources.
RecyclerView calls this method right before clearing ViewHolder's internal data and sending it to RecycledViewPool. This way, if ViewHolder was holding valid information before being recycled, you can call getBindingAdapterPosition to get its adapter position.
| Parameters | |
|---|---|
holder: FragmentViewHolder |
The ViewHolder for the view being recycled |
registerFragmentTransactionCallback
fun registerFragmentTransactionCallback(
callback: FragmentStateAdapter.FragmentTransactionCallback
): Unit
Registers a FragmentTransactionCallback to listen to fragment lifecycle changes that happen inside the adapter.
| Parameters | |
|---|---|
callback: FragmentStateAdapter.FragmentTransactionCallback |
Callback to register |
setHasStableIds
fun setHasStableIds(hasStableIds: Boolean): Unit
Indicates whether each item in the data set can be represented with a unique identifier of type java.lang.Long.
| Parameters | |
|---|---|
hasStableIds: Boolean |
Whether items in data set have unique identifiers or not. |
| See also | |
|---|---|
hasStableIds |
|
getItemId |
unregisterFragmentTransactionCallback
fun unregisterFragmentTransactionCallback(
callback: FragmentStateAdapter.FragmentTransactionCallback
): Unit
Unregisters a FragmentTransactionCallback.
| Parameters | |
|---|---|
callback: FragmentStateAdapter.FragmentTransactionCallback |
Callback to unregister |
| See also | |
|---|---|
registerFragmentTransactionCallback |