PagerAdapter

abstract class PagerAdapter(source)

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 indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem and/or destroyItem will follow, and the end of an update will be signaled by a call to finishUpdate. 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 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 after creation and adding them to the parent ViewGroup. A matching destroyItem implementation would remove the View from the parent ViewGroup and isViewFromObject 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.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
val POSITION_NONE: Int = -2
Link copied to clipboard

Functions

Link copied to clipboard
open fun destroyItem(@NonNull container: View, position: Int, @NonNull object: Any)
open fun destroyItem(@NonNull container: ViewGroup, position: Int, @NonNull object: Any)
Remove a page for the given position.
Link copied to clipboard
open fun finishUpdate(@NonNull container: View)
open fun finishUpdate(@NonNull container: ViewGroup)
Called when the a change in the shown pages has been completed.
Link copied to clipboard
abstract fun getCount(): Int
Return the number of views available.
Link copied to clipboard
open fun getItemPosition(@NonNull object: Any): Int
Called when the host view is attempting to determine if an item's position has changed.
Link copied to clipboard
open fun getPageTitle(position: Int): CharSequence
This method may be called by the ViewPager to obtain a title string to describe the specified page.
Link copied to clipboard
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.
Link copied to clipboard
open fun instantiateItem(@NonNull container: View, position: Int): Any
open fun instantiateItem(@NonNull container: ViewGroup, position: Int): Any
Create the page for the given position.
Link copied to clipboard
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.
Link copied to clipboard
This method should be called by the application if the data backing this adapter has changed and associated views should update.
Link copied to clipboard
Register an observer to receive callbacks related to the adapter's data changing.
Link copied to clipboard
open fun restoreState(@Nullable state: Parcelable, @Nullable loader: ClassLoader)
Restore any instance state associated with this adapter and its pages that was previously saved by saveState.
Link copied to clipboard
Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.
Link copied to clipboard
open fun setPrimaryItem(@NonNull container: View, position: Int, @NonNull object: Any)
open fun 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.
Link copied to clipboard
open fun startUpdate(@NonNull container: View)
open fun startUpdate(@NonNull container: ViewGroup)
Called when a change in the shown pages is going to start being made.
Link copied to clipboard
Unregister an observer from callbacks related to the adapter's data changing.