Register now for Android Dev Summit 2019!

PagerAdapter

abstract class PagerAdapter
kotlin.Any
   ↳ androidx.viewpager.widget.PagerAdapter

Base class providing the adapter to populate pages inside of a ViewPager. You will most likely want to use a more specific implementation of this, such as androidx.fragment.app.FragmentPagerAdapter or androidx.fragment.app.FragmentStatePagerAdapter.

When you implement a PagerAdapter, you must override the following methods at minimum:

PagerAdapter is more general than the adapters used for AdapterViews. Instead of providing a View recycling mechanism directly ViewPager uses callbacks to indicate the steps taken during an update. A PagerAdapter may implement a form of View recycling if desired or use a more sophisticated method of managing page Views such as Fragment transactions where each page is represented by its own Fragment.

ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter. A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem(ViewGroup, int) and/or destroyItem(ViewGroup, int, Object) will follow, and the end of an update will be signaled by a call to finishUpdate(ViewGroup). By the time finishUpdate returns the views associated with the key objects returned by instantiateItem should be added to the parent ViewGroup passed to these methods and the views associated with the keys passed to destroyItem should be removed. The method isViewFromObject(View, Object) identifies whether a page View is associated with a given key object.

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged() similar to AdapterView adapters derived from android.widget.BaseAdapter. A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter implements the method getItemPosition(Object).

Summary

Constants

static Int

static Int

Public constructors

Base class providing the adapter to populate pages inside of a ViewPager.

Public methods

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

Remove a page for the given position.

open Unit
destroyItem(@NonNull container: View, position: Int, @NonNull object: Any)

Remove a page for the given position.

open Unit
finishUpdate(@NonNull container: ViewGroup)

Called when the a change in the shown pages has been completed.

open Unit
finishUpdate(@NonNull container: View)

Called when the a change in the shown pages has been completed.

abstract Int

Return the number of views available.

open Int
getItemPosition(@NonNull object: Any)

Called when the host view is attempting to determine if an item's position has changed.

open CharSequence?
getPageTitle(position: Int)

This method may be called by the ViewPager to obtain a title string to describe the specified page.

open Float
getPageWidth(position: Int)

Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f]

open Any
instantiateItem(@NonNull container: ViewGroup, position: Int)

Create the page for the given position.

open Any
instantiateItem(@NonNull container: View, position: Int)

Create the page for the given position.

abstract Boolean
isViewFromObject(@NonNull view: View, @NonNull object: Any)

Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int).

open Unit

This method should be called by the application if the data backing this adapter has changed and associated views should update.

open Unit

Register an observer to receive callbacks related to the adapter's data changing.

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

Restore any instance state associated with this adapter and its pages that was previously saved by saveState().

open Parcelable?

Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.

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

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.

open Unit
setPrimaryItem(@NonNull container: View, position: Int, @NonNull object: Any)

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.

open Unit
startUpdate(@NonNull container: ViewGroup)

Called when a change in the shown pages is going to start being made.

open Unit
startUpdate(@NonNull container: View)

Called when a change in the shown pages is going to start being made.

open Unit

Unregister an observer from callbacks related to the adapter's data changing.

Constants

POSITION_NONE

static val POSITION_NONE: Int
Value: -2

POSITION_UNCHANGED

static val POSITION_UNCHANGED: Int
Value: -1

Public constructors

<init>

PagerAdapter()

Base class providing the adapter to populate pages inside of a ViewPager. You will most likely want to use a more specific implementation of this, such as androidx.fragment.app.FragmentPagerAdapter or androidx.fragment.app.FragmentStatePagerAdapter.

When you implement a PagerAdapter, you must override the following methods at minimum:

PagerAdapter is more general than the adapters used for AdapterViews. Instead of providing a View recycling mechanism directly ViewPager uses callbacks to indicate the steps taken during an update. A PagerAdapter may implement a form of View recycling if desired or use a more sophisticated method of managing page Views such as Fragment transactions where each page is represented by its own Fragment.

ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter. A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem(ViewGroup, int) and/or destroyItem(ViewGroup, int, Object) will follow, and the end of an update will be signaled by a call to finishUpdate(ViewGroup). By the time finishUpdate returns the views associated with the key objects returned by instantiateItem should be added to the parent ViewGroup passed to these methods and the views associated with the keys passed to destroyItem should be removed. The method isViewFromObject(View, Object) identifies whether a page View is associated with a given key object.

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged() similar to AdapterView adapters derived from android.widget.BaseAdapter. A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter implements the method getItemPosition(Object).

Public methods

destroyItem

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

Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

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

destroyItem

open fun destroyItem(@NonNull container: View, position: Int, @NonNull object: Any): Unit

Deprecated: Use destroyItem(ViewGroup, int, Object)

Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(View).

Parameters
container View: The containing View from which the page will be removed.
position View: The page position to be removed.
object View: The same object that was returned by instantiateItem(View, int).

finishUpdate

open fun finishUpdate(@NonNull container: ViewGroup): Unit

Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.

Parameters
container ViewGroup: The containing View which is displaying this adapter's page views.

finishUpdate

open fun finishUpdate(@NonNull container: View): Unit

Deprecated: Use finishUpdate(ViewGroup)

Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.

Parameters
container View: The containing View which is displaying this adapter's page views.

getCount

abstract fun getCount(): Int

Return the number of views available.

getItemPosition

open fun getItemPosition(@NonNull object: Any): Int

Called when the host view is attempting to determine if an item's position has changed. Returns POSITION_UNCHANGED if the position of the given item has not changed or POSITION_NONE if the item is no longer present in the adapter.

The default implementation assumes that items will never change position and always returns POSITION_UNCHANGED.

Parameters
object Any: Object representing an item, previously returned by a call to instantiateItem(View, int).
Return
Int: object's new position index from [0, getCount()), POSITION_UNCHANGED if the object's position has not changed, or POSITION_NONE if the item is no longer present.

getPageTitle

@Nullable open fun getPageTitle(position: Int): CharSequence?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method may return null indicating no title for this page. The default implementation returns null.

Parameters
position Int: The position of the title requested
Return
CharSequence?: A title for the requested page

getPageWidth

open fun getPageWidth(position: Int): Float

Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f]

Parameters
position Int: The position of the page requested
Return
Float: Proportional width for the given page position

instantiateItem

@NonNull open fun instantiateItem(@NonNull container: ViewGroup, position: Int): Any

Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

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.

instantiateItem

@NonNull open fun instantiateItem(@NonNull container: View, position: Int): Any

Deprecated: Use instantiateItem(ViewGroup, int)

Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

Parameters
container View: The containing View in which the page will be shown.
position View: 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.

isViewFromObject

abstract fun isViewFromObject(@NonNull view: View, @NonNull object: Any): Boolean

Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.

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

notifyDataSetChanged

open fun notifyDataSetChanged(): Unit

This method should be called by the application if the data backing this adapter has changed and associated views should update.

registerDataSetObserver

open fun registerDataSetObserver(@NonNull observer: DataSetObserver): Unit

Register an observer to receive callbacks related to the adapter's data changing.

Parameters
observer DataSetObserver: The android.database.DataSetObserver which will receive callbacks.

restoreState

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

Restore any instance state associated with this adapter and its pages that was previously saved by saveState().

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

@Nullable open fun saveState(): Parcelable?

Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.

Return
Parcelable?: Saved state for this adapter

setPrimaryItem

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

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page. This method will not be invoked when the adapter contains no items.

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

setPrimaryItem

open fun setPrimaryItem(@NonNull container: View, position: Int, @NonNull object: Any): Unit

Deprecated: Use setPrimaryItem(ViewGroup, int, Object)

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.

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

startUpdate

open fun startUpdate(@NonNull container: ViewGroup): Unit

Called when a change in the shown pages is going to start being made.

Parameters
container ViewGroup: The containing View which is displaying this adapter's page views.

startUpdate

open fun startUpdate(@NonNull container: View): Unit

Deprecated: Use startUpdate(ViewGroup)

Called when a change in the shown pages is going to start being made.

Parameters
container View: The containing View which is displaying this adapter's page views.

unregisterDataSetObserver

open fun unregisterDataSetObserver(@NonNull observer: DataSetObserver): Unit

Unregister an observer from callbacks related to the adapter's data changing.

Parameters
observer DataSetObserver: The android.database.DataSetObserver which will be unregistered.