-
public abstract class 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
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.
-
-
Field Summary
Fields Modifier and Type Field Description public final static int
POSITION_UNCHANGED
public final static int
POSITION_NONE
-
Method Summary
Modifier and Type Method Description abstract int
getCount()
Return the number of views available. void
startUpdate(@NonNull() ViewGroup container)
Called when a change in the shown pages is going to start being made. Object
instantiateItem(@NonNull() ViewGroup container, int position)
Create the page for the given position. void
destroyItem(@NonNull() ViewGroup container, int position, @NonNull() Object object)
Remove a page for the given position. void
setPrimaryItem(@NonNull() ViewGroup container, int position, @NonNull() Object object)
Called to inform the adapter of which item is currently considered tobe 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. void
finishUpdate(@NonNull() ViewGroup container)
Called when the a change in the shown pages has been completed. void
startUpdate(@NonNull() View container)
Called when a change in the shown pages is going to start being made. Object
instantiateItem(@NonNull() View container, int position)
Create the page for the given position. void
destroyItem(@NonNull() View container, int position, @NonNull() Object object)
Remove a page for the given position. void
setPrimaryItem(@NonNull() View container, int position, @NonNull() Object object)
Called to inform the adapter of which item is currently considered tobe the "primary", that is the one show to the user as the current page. void
finishUpdate(@NonNull() View container)
Called when the a change in the shown pages has been completed. abstract boolean
isViewFromObject(@NonNull() View view, @NonNull() Object object)
Determines whether a page View is associated with a specific key objectas returned by instantiateItem. Parcelable
saveState()
Save any instance state associated with this adapter and its pages that should berestored if the current UI state needs to be reconstructed. void
restoreState(@Nullable() Parcelable state, @Nullable() ClassLoader loader)
Restore any instance state associated with this adapter and its pagesthat was previously saved by saveState. int
getItemPosition(@NonNull() Object object)
Called when the host view is attempting to determine if an item's positionhas changed. void
notifyDataSetChanged()
This method should be called by the application if the data backing this adapter has changedand associated views should update. void
registerDataSetObserver(@NonNull() DataSetObserver observer)
Register an observer to receive callbacks related to the adapter's data changing. void
unregisterDataSetObserver(@NonNull() DataSetObserver observer)
Unregister an observer from callbacks related to the adapter's data changing. CharSequence
getPageTitle(int position)
This method may be called by the ViewPager to obtain a title stringto describe the specified page. float
getPageWidth(int position)
Returns the proportional width of a given page as a percentage of theViewPager's measured width from (0.f-1. -
-
Method Detail
-
getCount
abstract int getCount()
Return the number of views available.
-
startUpdate
void startUpdate(@NonNull() ViewGroup container)
Called when a change in the shown pages is going to start being made.
- Parameters:
container
- The containing View which is displaying this adapter'spage views.
-
instantiateItem
@NonNull() Object instantiateItem(@NonNull() ViewGroup container, int position)
Create the page for the given position. The adapter is responsiblefor adding the view to the container given here, although it onlymust ensure this is done by the time it returns from finishUpdate.
- Parameters:
container
- The containing View in which the page will be shown.position
- The page position to be instantiated.
-
destroyItem
void destroyItem(@NonNull() ViewGroup container, int position, @NonNull() Object object)
Remove a page for the given position. The adapter is responsiblefor removing the view from its container, although it only must ensurethis is done by the time it returns from finishUpdate.
- Parameters:
container
- The containing View from which the page will be removed.position
- The page position to be removed.object
- The same object that was returned by instantiateItem.
-
setPrimaryItem
void setPrimaryItem(@NonNull() ViewGroup container, int position, @NonNull() Object object)
Called to inform the adapter of which item is currently considered tobe 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
- The containing View from which the page will be removed.position
- The page position that is now the primary.object
- The same object that was returned by instantiateItem.
-
finishUpdate
void finishUpdate(@NonNull() ViewGroup container)
Called when the a change in the shown pages has been completed. At thispoint you must ensure that all of the pages have actually been added orremoved from the container as appropriate.
- Parameters:
container
- The containing View which is displaying this adapter'spage views.
-
startUpdate
@Deprecated() void startUpdate(@NonNull() View container)
Called when a change in the shown pages is going to start being made.
- Parameters:
container
- The containing View which is displaying this adapter'spage views.
-
instantiateItem
@Deprecated()@NonNull() Object instantiateItem(@NonNull() View container, int position)
Create the page for the given position. The adapter is responsiblefor adding the view to the container given here, although it onlymust ensure this is done by the time it returns from finishUpdate.
- Parameters:
container
- The containing View in which the page will be shown.position
- The page position to be instantiated.
-
destroyItem
@Deprecated() void destroyItem(@NonNull() View container, int position, @NonNull() Object object)
Remove a page for the given position. The adapter is responsiblefor removing the view from its container, although it only must ensurethis is done by the time it returns from finishUpdate.
- Parameters:
container
- The containing View from which the page will be removed.position
- The page position to be removed.object
- The same object that was returned by instantiateItem.
-
setPrimaryItem
@Deprecated() void setPrimaryItem(@NonNull() View container, int position, @NonNull() Object object)
Called to inform the adapter of which item is currently considered tobe the "primary", that is the one show to the user as the current page.
- Parameters:
container
- The containing View from which the page will be removed.position
- The page position that is now the primary.object
- The same object that was returned by instantiateItem.
-
finishUpdate
@Deprecated() void finishUpdate(@NonNull() View container)
Called when the a change in the shown pages has been completed. At thispoint you must ensure that all of the pages have actually been added orremoved from the container as appropriate.
- Parameters:
container
- The containing View which is displaying this adapter'spage views.
-
isViewFromObject
abstract boolean isViewFromObject(@NonNull() View view, @NonNull() Object object)
Determines whether a page View is associated with a specific key objectas returned by instantiateItem. This method isrequired for a PagerAdapter to function properly.
- Parameters:
view
- Page View to check for association withobject
object
- Object to check for association withview
-
saveState
@Nullable() Parcelable saveState()
Save any instance state associated with this adapter and its pages that should berestored if the current UI state needs to be reconstructed.
-
restoreState
void restoreState(@Nullable() Parcelable state, @Nullable() ClassLoader loader)
Restore any instance state associated with this adapter and its pagesthat was previously saved by saveState.
- Parameters:
state
- State previously saved by a call to saveStateloader
- A ClassLoader that should be used to instantiate any restored objects
-
getItemPosition
int getItemPosition(@NonNull() Object object)
Called when the host view is attempting to determine if an item's positionhas changed. Returns POSITION_UNCHANGED if the position of the givenitem has not changed or POSITION_NONE if the item is no longer presentin the adapter.
The default implementation assumes that items will neverchange position and always returns POSITION_UNCHANGED.
- Parameters:
object
- Object representing an item, previously returned by a call toinstantiateItem.
-
notifyDataSetChanged
void notifyDataSetChanged()
This method should be called by the application if the data backing this adapter has changedand associated views should update.
-
registerDataSetObserver
void registerDataSetObserver(@NonNull() DataSetObserver observer)
Register an observer to receive callbacks related to the adapter's data changing.
- Parameters:
observer
- The android.database.DataSetObserver which will receive callbacks.
-
unregisterDataSetObserver
void unregisterDataSetObserver(@NonNull() DataSetObserver observer)
Unregister an observer from callbacks related to the adapter's data changing.
- Parameters:
observer
- The android.database.DataSetObserver which will be unregistered.
-
getPageTitle
@Nullable() CharSequence getPageTitle(int position)
This method may be called by the ViewPager to obtain a title stringto describe the specified page. This method may return nullindicating no title for this page. The default implementation returnsnull.
- Parameters:
position
- The position of the title requested
-
getPageWidth
float getPageWidth(int position)
Returns the proportional width of a given page as a percentage of theViewPager's measured width from (0.f-1.f]
- Parameters:
position
- The position of the page requested
-
-
-
-