android.support.v4.app
Class FragmentPagerAdapter

java.lang.Object
  extended by android.support.v4.view.PagerAdapter
      extended by android.support.v4.app.FragmentPagerAdapter

public abstract class FragmentPagerAdapter
extends PagerAdapter

Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

This version of the pager 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.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement getItem(int) and PagerAdapter.getCount() to have a working adapter.

Here is an example implementation of a pager containing fragments of lists:

The R.layout.fragment_pager resource of the top-level fragment is:

The R.layout.fragment_pager_list resource containing each individual fragment's layout is:


Field Summary
 
Fields inherited from class android.support.v4.view.PagerAdapter
POSITION_NONE, POSITION_UNCHANGED
 
Constructor Summary
FragmentPagerAdapter(FragmentManager fm)
           
 
Method Summary
 void destroyItem(android.view.ViewGroup container, int position, Object object)
          Remove a page for the given position.
 void finishUpdate(android.view.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.
 long getItemId(int position)
          Return a unique identifier for the item at the given position.
 Object instantiateItem(android.view.ViewGroup container, int position)
          Create the page for the given position.
 boolean isViewFromObject(android.view.View view, Object object)
          Determines whether a page View is associated with a specific key object as returned by PagerAdapter.instantiateItem(ViewGroup, int).
 void restoreState(android.os.Parcelable state, ClassLoader loader)
          Restore any instance state associated with this adapter and its pages that was previously saved by PagerAdapter.saveState().
 android.os.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(android.view.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(android.view.ViewGroup container)
          Called when a change in the shown pages is going to start being made.
 
Methods inherited from class android.support.v4.view.PagerAdapter
destroyItem, finishUpdate, getCount, getItemPosition, getPageTitle, getPageWidth, instantiateItem, notifyDataSetChanged, registerDataSetObserver, setPrimaryItem, startUpdate, unregisterDataSetObserver
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FragmentPagerAdapter

public FragmentPagerAdapter(FragmentManager fm)
Method Detail

getItem

public abstract Fragment getItem(int position)
Return the Fragment associated with a specified position.


startUpdate

public void startUpdate(android.view.ViewGroup container)
Description copied from class: PagerAdapter
Called when a change in the shown pages is going to start being made.

Overrides:
startUpdate in class PagerAdapter
Parameters:
container - The containing View which is displaying this adapter's page views.

instantiateItem

public Object instantiateItem(android.view.ViewGroup container,
                              int position)
Description copied from class: PagerAdapter
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 PagerAdapter.finishUpdate(ViewGroup).

Overrides:
instantiateItem in class PagerAdapter
Parameters:
container - The containing View in which the page will be shown.
position - The page position to be instantiated.
Returns:
Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

destroyItem

public void destroyItem(android.view.ViewGroup container,
                        int position,
                        Object object)
Description copied from class: PagerAdapter
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 PagerAdapter.finishUpdate(ViewGroup).

Overrides:
destroyItem in class PagerAdapter
Parameters:
container - The containing View from which the page will be removed.
position - The page position to be removed.
object - The same object that was returned by PagerAdapter.instantiateItem(View, int).

setPrimaryItem

public void setPrimaryItem(android.view.ViewGroup container,
                           int position,
                           Object object)
Description copied from class: PagerAdapter
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.

Overrides:
setPrimaryItem in class PagerAdapter
Parameters:
container - The containing View from which the page will be removed.
position - The page position that is now the primary.
object - The same object that was returned by PagerAdapter.instantiateItem(View, int).

finishUpdate

public void finishUpdate(android.view.ViewGroup container)
Description copied from class: PagerAdapter
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.

Overrides:
finishUpdate in class PagerAdapter
Parameters:
container - The containing View which is displaying this adapter's page views.

isViewFromObject

public boolean isViewFromObject(android.view.View view,
                                Object object)
Description copied from class: PagerAdapter
Determines whether a page View is associated with a specific key object as returned by PagerAdapter.instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.

Specified by:
isViewFromObject in class PagerAdapter
Parameters:
view - Page View to check for association with object
object - Object to check for association with view
Returns:
true if view is associated with the key object object

saveState

public android.os.Parcelable saveState()
Description copied from class: PagerAdapter
Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.

Overrides:
saveState in class PagerAdapter
Returns:
Saved state for this adapter

restoreState

public void restoreState(android.os.Parcelable state,
                         ClassLoader loader)
Description copied from class: PagerAdapter
Restore any instance state associated with this adapter and its pages that was previously saved by PagerAdapter.saveState().

Overrides:
restoreState in class PagerAdapter
Parameters:
state - State previously saved by a call to PagerAdapter.saveState()
loader - A ClassLoader that should be used to instantiate any restored objects

getItemId

public long getItemId(int position)
Return a unique identifier for the item at the given position.

The default implementation returns the given position. Subclasses should override this method if the positions of items can change.

Parameters:
position - Position within this adapter
Returns:
Unique identifier for the item at position


Copyright © 2013 Marek Kedzierski. All Rights Reserved.