Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

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.

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

abstract Fragment
getItem(position: Int)

Provide a Fragment associated with the specified position.

open Unit
restoreState(@NonNull savedState: Parcelable)

open Parcelable

open Long
getItemId(position: Int)

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

Unit
setHasStableIds(hasStableIds: Boolean)

Boolean

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

Unit

Unit

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)

onCreateViewHolder

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

getItem

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

Provide a Fragment associated with the specified position.

restoreState

open fun restoreState(@NonNull savedState: Parcelable): Unit

saveState

@NonNull open fun saveState(): Parcelable

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

setHasStableIds

fun setHasStableIds(hasStableIds: Boolean): Unit

onFailedToRecycleView

fun onFailedToRecycleView(@NonNull holder: FragmentViewHolder): Boolean

onBindViewHolder

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

onViewRecycled

fun onViewRecycled(@NonNull holder: FragmentViewHolder): Unit

onViewAttachedToWindow

fun onViewAttachedToWindow(@NonNull holder: FragmentViewHolder): Unit