public
abstract
class
FragmentStatePagerAdapter
extends PagerAdapter
java.lang.Object | ||
↳ | android.support.v4.view.PagerAdapter | |
↳ | android.support.v4.app.FragmentStatePagerAdapter |
Implementation of PagerAdapter
that
uses a Fragment
to manage each page. This class also handles
saving and restoring of fragment's state.
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the saved
state of that fragment. This allows 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.
When using FragmentPagerAdapter the host ViewPager must have a valid ID set.
Subclasses only need to implement getItem(int)
and getCount()
to have a working adapter.
Here is an example implementation of a pager containing fragments of lists:
public class FragmentStatePagerSupport extends Activity { static final int NUM_ITEMS = 10; MyAdapter mAdapter; ViewPager mPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); // Watch for button clicks. Button button = (Button)findViewById(R.id.goto_first); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { mPager.setCurrentItem(0); } }); button = (Button)findViewById(R.id.goto_last); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { mPager.setCurrentItem(NUM_ITEMS-1); } }); } public static class MyAdapter extends FragmentStatePagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { return ArrayListFragment.newInstance(position); } } public static class ArrayListFragment extends ListFragment { int mNum; /** * Create a new instance of CountingFragment, providing "num" * as an argument. */ static ArrayListFragment newInstance(int num) { ArrayListFragment f = new ArrayListFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } /** * When creating, retrieve this instance's number from its arguments. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } /** * The Fragment's UI is just a simple text view showing its * instance number. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_pager_list, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText("Fragment #" + mNum); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { Log.i("FragmentList", "Item clicked: " + id); } } }
The R.layout.fragment_pager
resource of the top-level fragment is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1"> </android.support.v4.view.ViewPager> <LinearLayout android:orientation="horizontal" android:gravity="center" android:measureWithLargestChild="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"> <Button android:id="@+id/goto_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/first"> </Button> <Button android:id="@+id/goto_last" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/last"> </Button> </LinearLayout> </LinearLayout>
The R.layout.fragment_pager_list
resource containing each
individual fragment's layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:drawable/gallery_thumb"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/hello_world"/> <!-- The frame layout is here since we will be showing either the empty view or the list view. --> <FrameLayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > <!-- Here is the list. Since we are using a ListActivity, we have to call it "@android:id/list" so ListActivity will find it --> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false"/> <!-- Here is the view to show if the list is emtpy --> <TextView android:id="@android:id/empty" android:layout_width="match_parent" android:layout_height="match_parent" android:textAppearance="?android:attr/textAppearanceMedium" android:text="No items."/> </FrameLayout> </LinearLayout>
Inherited constants |
---|
From
class
android.support.v4.view.PagerAdapter
|
Public constructors | |
---|---|
FragmentStatePagerAdapter(FragmentManager fm)
|
Public methods | |
---|---|
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. |
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 |
void
|
restoreState(Parcelable state, ClassLoader loader)
Restore any instance state associated with this adapter and its pages
that was previously saved by |
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. |
Inherited methods | |
---|---|
From
class
android.support.v4.view.PagerAdapter
| |
From
class
java.lang.Object
|
FragmentStatePagerAdapter (FragmentManager fm)
Parameters | |
---|---|
fm |
FragmentManager
|
void destroyItem (ViewGroup container, int position, Object object)
Remove a page for the given position. The adapter is responsible
for removing the view from its container, although it only must ensure
this is done by the time it returns from finishUpdate(ViewGroup)
.
Parameters | |
---|---|
container |
ViewGroup :
The containing View from which the page will be removed. |
position |
int :
The page position to be removed. |
object |
Object :
The same object that was returned by
instantiateItem(View, int) .
|
void finishUpdate (ViewGroup container)
Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.
Parameters | |
---|---|
container |
ViewGroup :
The containing View which is displaying this adapter's
page views.
|
Fragment getItem (int position)
Return the Fragment associated with a specified position.
Parameters | |
---|---|
position |
int
|
Returns | |
---|---|
Fragment |
Object instantiateItem (ViewGroup container, int position)
Create the page for the given position. The adapter is responsible
for adding the view to the container given here, although it only
must ensure this is done by the time it returns from
finishUpdate(ViewGroup)
.
Parameters | |
---|---|
container |
ViewGroup :
The containing View in which the page will be shown. |
position |
int :
The page position to be instantiated. |
Returns | |
---|---|
Object |
Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page. |
boolean isViewFromObject (View view, Object object)
Determines whether a page View is associated with a specific key object
as returned by instantiateItem(ViewGroup, int)
. This method is
required for a PagerAdapter to function properly.
Parameters | |
---|---|
view |
View :
Page View to check for association with object |
object |
Object :
Object to check for association with view |
Returns | |
---|---|
boolean |
true if view is associated with the key object object
|
void restoreState (Parcelable state, ClassLoader loader)
Restore any instance state associated with this adapter and its pages
that was previously saved by saveState()
.
Parameters | |
---|---|
state |
Parcelable :
State previously saved by a call to saveState() |
loader |
ClassLoader :
A ClassLoader that should be used to instantiate any restored objects
|
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.
Returns | |
---|---|
Parcelable |
Saved state for this adapter |
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.
Parameters | |
---|---|
container |
ViewGroup :
The containing View from which the page will be removed. |
position |
int :
The page position that is now the primary. |
object |
Object :
The same object that was returned by
instantiateItem(View, int) .
|
void startUpdate (ViewGroup container)
Called when a change in the shown pages is going to start being made.
Parameters | |
---|---|
container |
ViewGroup :
The containing View which is displaying this adapter's
page views.
|