PagerAdapter
public abstract class PagerAdapter
FragmentPagerAdapter |
This class is deprecated. Switch to |
FragmentStatePagerAdapter |
This class is deprecated. Switch to |
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.
Summary
Constants |
|
|---|---|
static final int |
POSITION_NONE = -2 |
static final int |
POSITION_UNCHANGED = -1 |
Public constructors |
|---|
Public methods |
|
|---|---|
void |
This method is deprecated. Use |
void |
destroyItem(Remove a page for the given position. |
void |
This method is deprecated. Use |
void |
finishUpdate(@NonNull ViewGroup container)Called when the a change in the shown pages has been completed. |
abstract int |
getCount()Return the number of views available. |
int |
getItemPosition(@NonNull Object object)Called when the host view is attempting to determine if an item's position has changed. |
@Nullable CharSequence |
getPageTitle(int position)This method may be called by the ViewPager to obtain a title string to describe the specified page. |
float |
getPageWidth(int position)Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f] |
@NonNull Object |
This method is deprecated. Use |
@NonNull Object |
instantiateItem(@NonNull ViewGroup container, int position)Create the page for the given position. |
abstract boolean |
isViewFromObject(@NonNull View view, @NonNull Object object)Determines whether a page View is associated with a specific key object as returned by |
void |
This method should be called by the application if the data backing this adapter has changed and associated views should update. |
void |
registerDataSetObserver(@NonNull DataSetObserver observer)Register an observer to receive callbacks related to the adapter's data changing. |
void |
restoreState(@Nullable Parcelable state, @Nullable ClassLoader loader)Restore any instance state associated with this adapter and its pages that was previously saved by |
@Nullable 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. |
void |
This method is deprecated. Use |
void |
setPrimaryItem(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. |
void |
This method is deprecated. Use |
void |
startUpdate(@NonNull ViewGroup container)Called when a change in the shown pages is going to start being made. |
void |
unregisterDataSetObserver(@NonNull DataSetObserver observer)Unregister an observer from callbacks related to the adapter's data changing. |
Constants
Public constructors
Public methods
public voiddestroyItem(@NonNull View container, int position, @NonNull Object 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.
| Parameters | |
|---|---|
@NonNull View container |
The containing View from which the page will be removed. |
int position |
The page position to be removed. |
@NonNull Object object |
The same object that was returned by |
destroyItem
public void destroyItem(
@NonNull ViewGroup container,
int position,
@NonNull Object 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.
| Parameters | |
|---|---|
@NonNull ViewGroup container |
The containing View from which the page will be removed. |
int position |
The page position to be removed. |
@NonNull Object object |
The same object that was returned by |
public voidfinishUpdate(@NonNull View container)
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.
finishUpdate
public void finishUpdate(@NonNull ViewGroup container)
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.
getItemPosition
public int getItemPosition(@NonNull Object object)
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 | |
|---|---|
@NonNull Object object |
Object representing an item, previously returned by a call to |
| Returns | |
|---|---|
int |
object's new position index from [0, |
getPageTitle
public @Nullable CharSequence getPageTitle(int position)
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 | |
|---|---|
int position |
The position of the title requested |
| Returns | |
|---|---|
@Nullable CharSequence |
A title for the requested page |
getPageWidth
public float getPageWidth(int position)
Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f]
| Parameters | |
|---|---|
int position |
The position of the page requested |
| Returns | |
|---|---|
float |
Proportional width for the given page position |
public @NonNull ObjectinstantiateItem(@NonNull View container, int position)
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.
instantiateItem
public @NonNull Object instantiateItem(@NonNull ViewGroup container, int position)
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.
isViewFromObject
public abstract boolean isViewFromObject(@NonNull View view, @NonNull Object object)
Determines whether a page View is associated with a specific key object as returned by instantiateItem. This method is required for a PagerAdapter to function properly.
| Parameters | |
|---|---|
@NonNull View view |
Page View to check for association with |
@NonNull Object object |
Object to check for association with |
| Returns | |
|---|---|
boolean |
true if |
notifyDataSetChanged
public void notifyDataSetChanged()
This method should be called by the application if the data backing this adapter has changed and associated views should update.
registerDataSetObserver
public void registerDataSetObserver(@NonNull DataSetObserver observer)
Register an observer to receive callbacks related to the adapter's data changing.
| Parameters | |
|---|---|
@NonNull DataSetObserver observer |
The |
restoreState
public void restoreState(@Nullable Parcelable state, @Nullable ClassLoader loader)
Restore any instance state associated with this adapter and its pages that was previously saved by saveState.
| Parameters | |
|---|---|
@Nullable Parcelable state |
State previously saved by a call to |
@Nullable ClassLoader loader |
A ClassLoader that should be used to instantiate any restored objects |
saveState
public @Nullable Parcelable saveState()
Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.
| Returns | |
|---|---|
@Nullable Parcelable |
Saved state for this adapter |
public voidsetPrimaryItem(
@NonNull View container,
int position,
@NonNull Object 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 | |
|---|---|
@NonNull View container |
The containing View from which the page will be removed. |
int position |
The page position that is now the primary. |
@NonNull Object object |
The same object that was returned by |
setPrimaryItem
public void setPrimaryItem(
@NonNull ViewGroup container,
int position,
@NonNull Object 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. This method will not be invoked when the adapter contains no items.
| Parameters | |
|---|---|
@NonNull ViewGroup container |
The containing View from which the page will be removed. |
int position |
The page position that is now the primary. |
@NonNull Object object |
The same object that was returned by |
public voidstartUpdate(@NonNull View container)
Called when a change in the shown pages is going to start being made.
startUpdate
public void startUpdate(@NonNull ViewGroup container)
Called when a change in the shown pages is going to start being made.
unregisterDataSetObserver
public void unregisterDataSetObserver(@NonNull DataSetObserver observer)
Unregister an observer from callbacks related to the adapter's data changing.
| Parameters | |
|---|---|
@NonNull DataSetObserver observer |
The |