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

FragmentPagerAdapter

abstract class FragmentPagerAdapter : PagerAdapter
kotlin.Any
   ↳ androidx.viewpager.widget.PagerAdapter
   ↳ androidx.legacy.app.FragmentPagerAdapter

Implementation of PagerAdapter that represents each page as a android.app.Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement getItem(int) and getCount() to have a working adapter.

Here is an example implementation of a pager containing fragments of lists: {@sample frameworks/support/samples/Support13Demos/src/main/java/com/example/android/supportv13/app/FragmentPagerSupport.java * complete}

The R.layout.fragment_pager resource of the top-level fragment is: {@sample frameworks/support/samples/Support13Demos/src/main/res/layout/fragment_pager.xml * complete}

The R.layout.fragment_pager_list resource containing each individual fragment's layout is: {@sample frameworks/support/samples/Support13Demos/src/main/res/layout/fragment_pager_list.xml * complete}

Summary

Inherited constants
Public constructors

Use androidx.fragment.app.FragmentPagerAdapter instead.

Public methods
open Any
instantiateItem(container: ViewGroup, position: Int)

open Unit
finishUpdate(container: ViewGroup)

abstract Fragment!
getItem(position: Int)

Return the Fragment associated with a specified position.

open Unit
restoreState(state: Parcelable?, loader: ClassLoader?)

open Parcelable?

open Boolean
isViewFromObject(view: View, object: Any)

open Long
getItemId(position: Int)

Return a unique identifier for the item at the given position.

open Unit
startUpdate(container: ViewGroup)

open Unit
setPrimaryItem(container: ViewGroup, position: Int, object: Any)

open Unit
destroyItem(container: ViewGroup, position: Int, object: Any)

Inherited functions

Public constructors

<init>

FragmentPagerAdapter(fm: FragmentManager!)

Deprecated: Use androidx.fragment.app.FragmentPagerAdapter instead.

Public methods

instantiateItem

open fun instantiateItem(container: ViewGroup, position: Int): Any
Parameters
container ViewGroup: The containing View in which the page will be shown.
position ViewGroup: The page position to be instantiated.
Return
Any: Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

finishUpdate

open fun finishUpdate(container: ViewGroup): Unit
Parameters
container ViewGroup: The containing View which is displaying this adapter's page views.

getItem

abstract fun getItem(position: Int): Fragment!

Return the Fragment associated with a specified position.

restoreState

open fun restoreState(state: Parcelable?, loader: ClassLoader?): Unit
Parameters
state Parcelable?: State previously saved by a call to saveState()
loader Parcelable?: A ClassLoader that should be used to instantiate any restored objects

saveState

open fun saveState(): Parcelable?
Return
Parcelable?: Saved state for this adapter

isViewFromObject

open fun isViewFromObject(view: View, object: Any): Boolean
Parameters
view View: Page View to check for association with object
object View: Object to check for association with view
Return
Boolean: true if view is associated with the key object object

getItemId

open fun getItemId(position: Int): Long

Return a unique identifier for the item at the given position.

The default implementation returns the given position. Subclasses should override this method if the positions of items can change.

Parameters
position Int: Position within this adapter
Return
Long: Unique identifier for the item at position

startUpdate

open fun startUpdate(container: ViewGroup): Unit
Parameters
container ViewGroup: The containing View which is displaying this adapter's page views.

setPrimaryItem

open fun setPrimaryItem(container: ViewGroup, position: Int, object: Any): Unit
Parameters
container ViewGroup: The containing View from which the page will be removed.
position ViewGroup: The page position that is now the primary.
object ViewGroup: The same object that was returned by instantiateItem(View, int).

destroyItem

open fun destroyItem(container: ViewGroup, position: Int, object: Any): Unit
Parameters
container ViewGroup: The containing View from which the page will be removed.
position ViewGroup: The page position to be removed.
object ViewGroup: The same object that was returned by instantiateItem(View, int).