public abstract class FragmentPagerAdapter
extends androidx.viewpager.widget.PagerAdapter
PagerAdapter implementation that represents each page as a Fragment that is
persistently kept in the fragment manager as long as a user can return to the page.
This version of the pager adapter 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.
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
PagerAdapter.getCount() to become a fully working adapters.
| 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 |
|---|
FragmentPagerAdapter(androidx.fragment.app.FragmentManager fragmentManager)
Creates a new instance of FragmentPagerAdapter 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.
|
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 FragmentPagerAdapter(@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 enabled 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.PagerAdapter