Base class providing the adapter to populate pages inside of
a {@link ViewPager}. You will most likely want to use a more
specific implementation of this, such as
{@link android.support.v4.app.FragmentPagerAdapter} or
{@link android.support.v4.app.FragmentStatePagerAdapter}.
When you implement a PagerAdapter, you must override the following methods
at minimum:
- {@link #instantiateItem(ViewGroup, int)}
- {@link #destroyItem(ViewGroup, int, Object)}
- {@link #getCount()}
- {@link #isViewFromObject(View, Object)}
PagerAdapter is more general than the adapters used for
{@link android.widget.AdapterView 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
{@link #startUpdate(ViewGroup)} indicates that the contents of the ViewPager
are about to change. One or more calls to {@link #instantiateItem(ViewGroup, int)}
and/or {@link #destroyItem(ViewGroup, int, Object)} will follow, and the end
of an update will be signaled by a call to {@link #finishUpdate(ViewGroup)}.
By the time {@link #finishUpdate(ViewGroup) finishUpdate} returns the views
associated with the key objects returned by
{@link #instantiateItem(ViewGroup, int) instantiateItem} should be added to
the parent ViewGroup passed to these methods and the views associated with
the keys passed to {@link #destroyItem(ViewGroup, int, Object) destroyItem}
should be removed. The method {@link #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 {@link #instantiateItem(ViewGroup, int)}
after creation and adding them to the parent ViewGroup. A matching
{@link #destroyItem(ViewGroup, int, Object)} implementation would remove the
View from the parent ViewGroup and {@link #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 {@link #notifyDataSetChanged()} similar
to AdapterView adapters derived from {@link 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 {@link #getItemPosition(Object)}. |
Methods Summary |
---|
public void | destroyItem(android.view.ViewGroup container, int position, java.lang.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 {@link #finishUpdate(ViewGroup)}.
destroyItem((View) container, position, object);
|
public void | destroyItem(android.view.View container, int position, java.lang.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 {@link #finishUpdate(View)}.
throw new UnsupportedOperationException("Required method destroyItem was not overridden");
|
public void | finishUpdate(android.view.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.
|
public void | finishUpdate(android.view.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.
finishUpdate((View) container);
|
public abstract int | getCount()Return the number of views available.
|
public int | getItemPosition(java.lang.Object object)Called when the host view is attempting to determine if an item's position
has changed. Returns {@link #POSITION_UNCHANGED} if the position of the given
item has not changed or {@link #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 {@link #POSITION_UNCHANGED}.
return POSITION_UNCHANGED;
|
public java.lang.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.
return null;
|
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]
return 1.f;
|
public java.lang.Object | instantiateItem(android.view.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
{@link #finishUpdate(ViewGroup)}.
return instantiateItem((View) container, position);
|
public java.lang.Object | instantiateItem(android.view.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
{@link #finishUpdate(ViewGroup)}.
throw new UnsupportedOperationException(
"Required method instantiateItem was not overridden");
|
public abstract boolean | isViewFromObject(android.view.View view, java.lang.Object object)Determines whether a page View is associated with a specific key object
as returned by {@link #instantiateItem(ViewGroup, int)}. This method is
required for a PagerAdapter to function properly.
|
public void | notifyDataSetChanged()This method should be called by the application if the data backing this adapter has changed
and associated views should update.
mObservable.notifyChanged();
|
public void | registerDataSetObserver(android.database.DataSetObserver observer)Register an observer to receive callbacks related to the adapter's data changing.
mObservable.registerObserver(observer);
|
public void | restoreState(android.os.Parcelable state, java.lang.ClassLoader loader)Restore any instance state associated with this adapter and its pages
that was previously saved by {@link #saveState()}.
|
public android.os.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.
return null;
|
public void | setPrimaryItem(android.view.View container, int position, java.lang.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.
|
public void | setPrimaryItem(android.view.ViewGroup container, int position, java.lang.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.
setPrimaryItem((View) container, position, object);
|
public void | startUpdate(android.view.ViewGroup container)Called when a change in the shown pages is going to start being made.
startUpdate((View) container);
|
public void | startUpdate(android.view.View container)Called when a change in the shown pages is going to start being made.
|
public void | unregisterDataSetObserver(android.database.DataSetObserver observer)Unregister an observer from callbacks related to the adapter's data changing.
mObservable.unregisterObserver(observer);
|