public abstract class FragmentPagerAdapter extends PagerAdapter
PagerAdapter
that
represents each page as a Fragment
that is persistently
kept in the fragment manager as long as the user can return to the page.
This version of the pager is best for use when there are a handful of
typically more static fragments to be paged through, such as a set of tabs.
The fragment of each page the user visits will be kept in memory, though its
view hierarchy may be destroyed when not visible. This can result in using
a significant amount of memory since fragment instances can hold on to an
arbitrary amount of state. For larger sets of pages, consider
FragmentStatePagerAdapter
.
When using FragmentPagerAdapter the host ViewPager must have a valid ID set.
Subclasses only need to implement getItem(int)
and PagerAdapter.getCount()
to have a working adapter.
Here is an example implementation of a pager containing fragments of lists:
The R.layout.fragment_pager
resource of the top-level fragment is:
The R.layout.fragment_pager_list
resource containing each
individual fragment's layout is:
POSITION_NONE, POSITION_UNCHANGED
Constructor and Description |
---|
FragmentPagerAdapter(FragmentManager fm) |
Modifier and Type | Method and Description |
---|---|
void |
destroyItem(ViewGroup container,
int position,
Object object)
Remove a page for the given position.
|
void |
finishUpdate(ViewGroup container)
Called when the a change in the shown pages has been completed.
|
abstract Fragment |
getItem(int position)
Return the Fragment associated with a specified position.
|
long |
getItemId(int position)
Return a unique identifier for the item at the given position.
|
Object |
instantiateItem(ViewGroup container,
int position)
Create the page for the given position.
|
boolean |
isViewFromObject(View view,
Object object)
Determines whether a page View is associated with a specific key object
as returned by
PagerAdapter.instantiateItem(ViewGroup, int) . |
void |
restoreState(Parcelable state,
ClassLoader loader)
Restore any instance state associated with this adapter and its pages
that was previously saved by
PagerAdapter.saveState() . |
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.
|
void |
setPrimaryItem(ViewGroup container,
int position,
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.
|
void |
startUpdate(ViewGroup container)
Called when a change in the shown pages is going to start being made.
|
destroyItem, finishUpdate, getCount, getItemPosition, getPageTitle, getPageWidth, instantiateItem, notifyDataSetChanged, setPrimaryItem, startUpdate
public FragmentPagerAdapter(FragmentManager fm)
public abstract Fragment getItem(int position)
public void startUpdate(ViewGroup container)
PagerAdapter
startUpdate
in class PagerAdapter
container
- The containing View which is displaying this adapter's
page views.public Object instantiateItem(ViewGroup container, int position)
PagerAdapter
PagerAdapter.finishUpdate(ViewGroup)
.instantiateItem
in class PagerAdapter
container
- The containing View in which the page will be shown.position
- The page position to be instantiated.public void destroyItem(ViewGroup container, int position, Object object)
PagerAdapter
PagerAdapter.finishUpdate(ViewGroup)
.destroyItem
in class PagerAdapter
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
PagerAdapter.instantiateItem(View, int)
.public void setPrimaryItem(ViewGroup container, int position, Object object)
PagerAdapter
setPrimaryItem
in class PagerAdapter
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
PagerAdapter.instantiateItem(View, int)
.public void finishUpdate(ViewGroup container)
PagerAdapter
finishUpdate
in class PagerAdapter
container
- The containing View which is displaying this adapter's
page views.public boolean isViewFromObject(View view, Object object)
PagerAdapter
PagerAdapter.instantiateItem(ViewGroup, int)
. This method is
required for a PagerAdapter to function properly.isViewFromObject
in class PagerAdapter
view
- Page View to check for association with object
object
- Object to check for association with view
view
is associated with the key object object
public Parcelable saveState()
PagerAdapter
saveState
in class PagerAdapter
public void restoreState(Parcelable state, ClassLoader loader)
PagerAdapter
PagerAdapter.saveState()
.restoreState
in class PagerAdapter
state
- State previously saved by a call to PagerAdapter.saveState()
loader
- A ClassLoader that should be used to instantiate any restored objectspublic long getItemId(int position)
The default implementation returns the given position. Subclasses should override this method if the positions of items can change.
position
- Position within this adapter