public abstract class FragmentStatePagerAdapter
extends PagerAdapter
PagerAdapter implementation that represents each page as a Fragment. This pager
adapter also handles saving and restoring state of its fragments.
This version of the pager adapter is more useful when there are a large number of pages, working
more like a list view. When pages are not visible to a user, their entire fragment may be destroyed,
only keeping the saved state of that fragment. This allows to the pager to hold on to much less
memory associated with each visited page as compared to FragmentPagerAdapter at the cost
of potentially more overhead when switching between pages.
Note, that when using FragmentStatePagerAdapter, the host ViewPager must have a valid ID set, otherwise this adapter implementation will throw an exception.
The inheritance hierarchies only need to implement getItem(int) and #getCount()
to become a fully working adapters.
Cursor it is essential
to override also getItemId(int) and provide item id for the requested position as this
adapter tracks and stores its created fragments by theirs associated item ids. Item ids are also
used to store states of the adapter's fragments. Also note, that #getItemPosition(Object)
method should be implemented in a way which can properly resolve whether a fragment should be
still visible at its current position, moved to a different position or to be completely removed
due to changes in the data set.| Modifier and Type | Field and Description |
|---|---|
static int |
NO_POSITION
Constant used to identify unspecified position.
|
| Constructor and Description |
|---|
FragmentStatePagerAdapter(FragmentManager fragmentManager)
Creates a new instance of FragmentStatePagerAdapter with the specified fragmentManager.
|
| Modifier and Type | Method and Description |
|---|---|
void |
destroyItem(android.view.ViewGroup container,
int position,
Object object) |
void |
finishUpdate(android.view.ViewGroup container) |
abstract Fragment |
getItem(int position)
Instantiates a new
Fragment associated with the specified position. |
long |
getItemId(int position)
Returns a unique identifier for the item at the specified position.
|
Fragment |
getPrimaryFragment()
Returns the fragment instance that has been set as primary item via
setPrimaryItem(ViewGroup, int, Object). |
int |
getPrimaryPosition()
Returns position of the current primary item.
|
Object |
instantiateItem(android.view.ViewGroup container,
int position) |
boolean |
isViewFromObject(android.view.View view,
Object object) |
protected String |
makeItemTag(int containerId,
long itemId)
Makes a tag for an item with the given itemId.
|
void |
restoreState(android.os.Parcelable state,
ClassLoader loader) |
android.os.Parcelable |
saveState() |
protected void |
setMakeItemTags(boolean makeItemTags)
Sets a boolean flag indicating whether this pager adapter should make tags for its items or
not.
|
void |
setPrimaryItem(android.view.ViewGroup container,
int position,
Object object) |
void |
startUpdate(android.view.ViewGroup container) |
public static final int NO_POSITION
public FragmentStatePagerAdapter(FragmentManager fragmentManager)
fragmentManager - The manager used to manage fragments provided by the pager adapter.public void startUpdate(android.view.ViewGroup container)
public Object instantiateItem(android.view.ViewGroup container, int position)
protected final void setMakeItemTags(boolean makeItemTags)
This feature is by default enabled.
makeItemTags - True to enable making of item tags, false otherwise.makeItemTag(int, long)protected String makeItemTag(int containerId, long itemId)
By default making of item tags is disabled and the default implementation makes tag in the following format:
"android:pager:CONTAINER_ID:ITEM_ID"This feature may be enabled/disabled via
setMakeItemTags(boolean).containerId - Id of the associated view pager container.itemId - Id of the item for which to create its corresponding tag.null if this adapter does not create tags for its items.public long getItemId(int position)
The default implementation returns the given position. The inheritance hierarchies should override this method if the positions of items can change.
position - Position from the range of size of this this adapter.public abstract Fragment getItem(int position)
Fragment associated with the specified position.instantiateItem(ViewGroup, int)public void setPrimaryItem(android.view.ViewGroup container,
int position,
Object object)
public Fragment getPrimaryFragment()
setPrimaryItem(ViewGroup, int, Object).null if no primary item has been set yet.getPrimaryPosition()public int getPrimaryPosition()
NO_POSITION if no primary item has been specified yet.getPrimaryFragment(),
setPrimaryItem(ViewGroup, int, Object)public boolean isViewFromObject(android.view.View view,
Object object)
public void destroyItem(android.view.ViewGroup container,
int position,
Object object)
public void finishUpdate(android.view.ViewGroup container)
public android.os.Parcelable saveState()
public void restoreState(android.os.Parcelable state,
ClassLoader loader)