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:

Summary

Public constructors

<init>(@NonNull fragmentActivity: FragmentActivity)

<init>(@NonNull fragment: Fragment)

<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
getItem(position: Int)

Provide a Fragment associated with the specified position.

open Long
getItemId(position: Int)

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

Unit
onBindViewHolder(@NonNull holder: FragmentViewHolder, position: Int)

FragmentViewHolder
onCreateViewHolder(@NonNull parent: ViewGroup, viewType: Int)

Boolean

Unit

Unit

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)

getItem

@NonNull abstract fun getItem(position: Int): Fragment

Provide a Fragment associated with the specified position.

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()

onBindViewHolder

fun onBindViewHolder(@NonNull holder: FragmentViewHolder, position: Int): Unit

onCreateViewHolder

@NonNull fun onCreateViewHolder(@NonNull parent: ViewGroup, viewType: Int): FragmentViewHolder

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

setHasStableIds

fun setHasStableIds(hasStableIds: Boolean): Unit