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 fragment.
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 FragmentPagerAdapter, 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.
| Constructor and Description |
|---|
FragmentStatePagerAdapter(FragmentManager fragmentManager)
Creates a new instance of FragmentStatePagerAdapter with the specified fragmentManager.
|
| Modifier and Type | Method and Description |
|---|---|
void |
destroyItem(ViewGroup container,
int position,
Object object) |
void |
finishUpdate(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.
|
Object |
instantiateItem(ViewGroup container,
int position) |
boolean |
isViewFromObject(View view,
Object object) |
protected String |
makeItemTag(int containerId,
long itemId)
Makes a tag for an item with the given itemId.
|
void |
restoreState(Parcelable state,
ClassLoader loader) |
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(ViewGroup container,
int position,
Object object) |
void |
startUpdate(ViewGroup container) |
public FragmentStatePagerAdapter(@NonNull FragmentManager fragmentManager)
fragmentManager - The manager used to manage fragments provided by the pager adapter.public void startUpdate(ViewGroup container)
protected final void setMakeItemTags(boolean makeItemTags)
This feature is by default disabled.
makeItemTags - True to enable making of item tags, false otherwise.makeItemTag(int, long)@Nullable protected String makeItemTag(@IdRes 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.@NonNull public abstract Fragment getItem(int position)
Fragment associated with the specified position.instantiateItem(ViewGroup, int)public void finishUpdate(ViewGroup container)
public Parcelable saveState()
public void restoreState(Parcelable state, ClassLoader loader)