added in version 22.1.0
belongs to Maven artifact


public class ViewPager
extends ViewGroup

   ↳ android.view.View
     ↳ android.view.ViewGroup

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are FragmentPagerAdapter and FragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.

Views which are annotated with the ViewPager.DecorView annotation are treated as part of the view pagers 'decor'. Each decor view's position can be controlled via its android:layout_gravity attribute. For example:


         android:layout_gravity="top" />


For more information about how to use ViewPager, read Creating Swipe Views with Tabs.

You can find examples of using ViewPager in the API 4+ Support Demos and API 13+ Support Demos sample code.


Nested classes

@interface ViewPager.DecorView

Annotation which allows marking of views to be decoration views when added to a view pager. 

class ViewPager.LayoutParams

Layout parameters that should be supplied for views added to a ViewPager. 

interface ViewPager.OnAdapterChangeListener

Callback interface for responding to adapter changes. 

interface ViewPager.OnPageChangeListener

Callback interface for responding to changing state of the selected page. 

interface ViewPager.PageTransformer

A PageTransformer is invoked whenever a visible/attached page is scrolled. 

class ViewPager.SavedState

This is the persistent state that is saved by ViewPager. 

class ViewPager.SimpleOnPageChangeListener

Simple implementation of the ViewPager.OnPageChangeListener interface with stub implementations of each method. 



Indicates that the pager is currently being dragged by the user.


Indicates that the pager is in an idle, settled state.


Indicates that the pager is in the process of settling to a final position.

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

ViewPager(Context context)
ViewPager(Context context, AttributeSet attrs)

Public methods

void addFocusables(ArrayList<View> views, int direction, int focusableMode)

We only want the current page that is being shown to be focusable.

void addOnAdapterChangeListener(ViewPager.OnAdapterChangeListener listener)

Add a listener that will be invoked whenever the adapter for this ViewPager changes.

void addOnPageChangeListener(ViewPager.OnPageChangeListener listener)

Add a listener that will be invoked whenever the page changes or is incrementally scrolled.

void addTouchables(ArrayList<View> views)

We only want the current page that is being shown to be touchable.

void addView(View child, int index, ViewGroup.LayoutParams params)
boolean arrowScroll(int direction)

Handle scrolling in response to a left or right arrow click.

boolean beginFakeDrag()

Start a fake drag of the pager.

boolean canScrollHorizontally(int direction)

Check if this ViewPager can be scrolled horizontally in a certain direction.

void clearOnPageChangeListeners()

Remove all listeners that are notified of any changes in scroll state or position.

void computeScroll()
boolean dispatchKeyEvent(KeyEvent event)
boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
void draw(Canvas canvas)
void endFakeDrag()

End a fake drag of the pager.

boolean executeKeyEvent(KeyEvent event)

You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.

void fakeDragBy(float xOffset)

Fake drag by an offset in pixels.

ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
PagerAdapter getAdapter()

Retrieve the current adapter supplying pages.

int getCurrentItem()
int getOffscreenPageLimit()

Returns the number of pages that will be retained to either side of the current page in the view hierarchy in an idle state.

int getPageMargin()

Return the margin between pages.

boolean isFakeDragging()

Returns true if a fake drag is in progress.

boolean onInterceptTouchEvent(MotionEvent ev)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()
boolean onTouchEvent(MotionEvent ev)
void removeOnAdapterChangeListener(ViewPager.OnAdapterChangeListener listener)

Remove a listener that was previously added via addOnAdapterChangeListener(OnAdapterChangeListener).

void removeOnPageChangeListener(ViewPager.OnPageChangeListener listener)

Remove a listener that was previously added via addOnPageChangeListener(OnPageChangeListener).

void removeView(View view)
void setAdapter(PagerAdapter adapter)

Set a PagerAdapter that will supply views for this pager as needed.

void setCurrentItem(int item)

Set the currently selected page.

void setCurrentItem(int item, boolean smoothScroll)