FragmentStateAdapter
abstract class FragmentStateAdapter : RecyclerView.Adapter<FragmentViewHolder!>, StatefulAdapter
kotlin.Any | ||
↳ | androidx.recyclerview.widget.RecyclerView.Adapter<androidx.viewpager2.adapter.FragmentViewHolder> | |
↳ | androidx.viewpager2.adapter.FragmentStateAdapter |
Similar in behavior to FragmentStatePagerAdapter
Lifecycle within RecyclerView
:
RecyclerView.ViewHolder
initially an emptyFrameLayout
, serves as a re-usable container for aFragment
in later stages.RecyclerView.Adapter#onBindViewHolder
we ask for aFragment
for the position. If we already have the fragment, or have previously saved its state, we use those.RecyclerView.Adapter#onAttachedToWindow
we attach theFragment
to a container.RecyclerView.Adapter#onViewRecycled
we remove, save state, destroy theFragment
.
Summary
Public constructors |
|
---|---|
<init>(@NonNull fragmentActivity: FragmentActivity) |
|
<init>(@NonNull fragmentManager: FragmentManager, @NonNull lifecycle: Lifecycle) |
Public methods |
|
---|---|
open 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. |
open Long |
Default implementation works for collections that don't add, move, remove items. |
open Unit |
onAttachedToRecyclerView(@NonNull recyclerView: RecyclerView) |
Unit |
onBindViewHolder(@NonNull holder: FragmentViewHolder, position: Int) |
FragmentViewHolder |
onCreateViewHolder(@NonNull parent: ViewGroup, viewType: Int) |
open Unit |
onDetachedFromRecyclerView(@NonNull recyclerView: RecyclerView) |
Boolean |
onFailedToRecycleView(@NonNull holder: FragmentViewHolder) |
Unit |
onViewAttachedToWindow(@NonNull holder: FragmentViewHolder) |
Unit |
onViewRecycled(@NonNull holder: FragmentViewHolder) |
Unit |
restoreState(@NonNull savedState: Parcelable) |
Parcelable | |
Unit |
setHasStableIds(hasStableIds: Boolean) |
Inherited functions |
|
---|---|
Public constructors
<init>
FragmentStateAdapter(@NonNull fragmentActivity: FragmentActivity)
Parameters | |
---|---|
fragmentActivity |
FragmentActivity: if the ViewPager2 lives directly in a FragmentActivity subclass. |
<init>
FragmentStateAdapter(@NonNull fragment: Fragment)
Parameters | |
---|---|
fragment |
Fragment: if the ViewPager2 lives directly in a Fragment subclass. |
<init>
FragmentStateAdapter(@NonNull fragmentManager: FragmentManager, @NonNull lifecycle: Lifecycle)
Parameters | |
---|---|
fragmentManager |
FragmentManager: of ViewPager2 's host |
lifecycle |
FragmentManager: of ViewPager2 's host |
Public methods
containsItem
open fun containsItem(itemId: Long): Boolean
Default implementation works for collections that don't add, move, remove items.
TODO(b/122670460): add lint rule When overriding, also override getItemId(int)
createFragment
@NonNull 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
getItemId
open fun getItemId(position: Int): Long
Default implementation works for collections that don't add, move, remove items.
TODO(b/122670460): add lint rule 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 |
Return | |
---|---|
Long: stable item id RecyclerView.Adapter#hasStableIds() |
onAttachedToRecyclerView
@CallSuper open fun onAttachedToRecyclerView(@NonNull recyclerView: RecyclerView): Unit
onBindViewHolder
fun onBindViewHolder(@NonNull holder: FragmentViewHolder, position: Int): Unit
onCreateViewHolder
@NonNull fun onCreateViewHolder(@NonNull parent: ViewGroup, viewType: Int): FragmentViewHolder
onDetachedFromRecyclerView
@CallSuper open fun onDetachedFromRecyclerView(@NonNull recyclerView: RecyclerView): Unit
onFailedToRecycleView
fun onFailedToRecycleView(@NonNull holder: FragmentViewHolder): Boolean
onViewAttachedToWindow
fun onViewAttachedToWindow(@NonNull holder: FragmentViewHolder): Unit
onViewRecycled
fun onViewRecycled(@NonNull holder: FragmentViewHolder): Unit
restoreState
fun restoreState(@NonNull savedState: Parcelable): Unit
saveState
@NonNull fun saveState(): Parcelable