public abstract class FragmentStatePagerAdapter
extends androidx.viewpager.widget.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 PagerAdapter.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 PagerAdapter.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 long |
NO_ID
Constant used to identify unspecified id.
|
static int |
NO_POSITION
Constant used to identify unspecified position.
|
| Constructor and Description |
|---|
FragmentStatePagerAdapter(androidx.fragment.app.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 androidx.fragment.app.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.
|
androidx.fragment.app.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 static final long NO_ID
public FragmentStatePagerAdapter(@NonNull
androidx.fragment.app.FragmentManager fragmentManager)
fragmentManager - The manager used to manage fragments provided by the pager adapter.public void startUpdate(@NonNull
android.view.ViewGroup container)
startUpdate in class androidx.viewpager.widget.PagerAdapter@NonNull public Object instantiateItem(@NonNull android.view.ViewGroup container, int position)
instantiateItem in class androidx.viewpager.widget.PagerAdapterprotected final void setMakeItemTags(boolean makeItemTags)
This feature is by default enabled.
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 they have better way to uniquely identify their items.
Note that this method may be called also from destroyItem(ViewGroup, int, Object)
when, possibly, the data set of this adapter may be already invalid. In such case it is safe
to return NO_ID.
position - Position from the range of size of items of this adapter.@NonNull public abstract androidx.fragment.app.Fragment getItem(int position)
Fragment associated with the specified position.instantiateItem(ViewGroup, int)public void setPrimaryItem(@NonNull
android.view.ViewGroup container,
int position,
@Nullable
Object object)
setPrimaryItem in class androidx.viewpager.widget.PagerAdapter@Nullable public androidx.fragment.app.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(@NonNull
android.view.View view,
@NonNull
Object object)
isViewFromObject in class androidx.viewpager.widget.PagerAdapterpublic void destroyItem(@NonNull
android.view.ViewGroup container,
int position,
@NonNull
Object object)
destroyItem in class androidx.viewpager.widget.PagerAdapterpublic void finishUpdate(@NonNull
android.view.ViewGroup container)
finishUpdate in class androidx.viewpager.widget.PagerAdapterpublic android.os.Parcelable saveState()
saveState in class androidx.viewpager.widget.PagerAdapterpublic void restoreState(@Nullable
android.os.Parcelable state,
@Nullable
ClassLoader loader)
restoreState in class androidx.viewpager.widget.PagerAdapter