public final class ViewPager2
extends ViewGroup

   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.viewpager2.widget.ViewPager2

ViewPager2 replaces ViewPager, addressing most of its predecessor’s pain-points, including right-to-left layout support, vertical orientation, modifiable Fragment collections, etc.

See also:


Nested classes

class ViewPager2.OnPageChangeCallback

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

interface ViewPager2.PageTransformer

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



Value to indicate that the default caching mechanism of RecyclerView should be used instead of explicitly prefetch and retain pages to either side of the current page.




Indicates that the ViewPager2 is currently being dragged by the user, or programmatically via fake drag functionality.


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


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

Inherited constants

Inherited fields

Public constructors

ViewPager2(Context context)
ViewPager2(Context context, AttributeSet attrs)
ViewPager2(Context context, AttributeSet attrs, int defStyleAttr)
ViewPager2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void addItemDecoration(RecyclerView.ItemDecoration decor)

Add an RecyclerView.ItemDecoration to this ViewPager2.

void addItemDecoration(RecyclerView.ItemDecoration decor, int index)

Add an RecyclerView.ItemDecoration to this ViewPager2.

boolean beginFakeDrag()

Start a fake drag of the pager.

boolean canScrollHorizontally(int direction)
boolean canScrollVertically(int direction)
boolean endFakeDrag()

End a fake drag of the pager.

boolean fakeDragBy(float offsetPxFloat)

Fake drag by an offset in pixels.

CharSequence getAccessibilityClassName()
Adapter getAdapter()
int getCurrentItem()

Returns the currently selected page.

RecyclerView.ItemDecoration getItemDecorationAt(int index)

Returns an RecyclerView.ItemDecoration previously added to this ViewPager2.

int getItemDecorationCount()

Returns the number of RecyclerView.ItemDecoration currently added to this ViewPager2.

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 getOrientation()
int getScrollState()

Returns the current scroll state of the ViewPager2.

void invalidateItemDecorations()

Invalidates all ItemDecorations.

boolean isFakeDragging()

Returns true if a fake drag is in progress.

boolean isUserInputEnabled()

Returns if user initiated scrolling between pages is enabled.

void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void onViewAdded(View child)
boolean performAccessibilityAction(int action, Bundle arguments)
void registerOnPageChangeCallback(ViewPager2.OnPageChangeCallback callback)

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

void removeItemDecoration(RecyclerView.ItemDecoration decor)

Remove an RecyclerView.ItemDecoration from this ViewPager2.

void removeItemDecorationAt(int index)

Removes the RecyclerView.ItemDecoration associated with the supplied index position.

void requestTransform()

Trigger a call to the registered PageTransformer's transformPage method.

void setAdapter(Adapter adapter)

Set a new adapter to provide page views on demand.

void setCurrentItem(int item)

Set the currently selected page.

void setCurrentItem(int item, boolean smoothScroll)

Set the currently selected page.

void setLayoutDirection(int layoutDirection)
void setOffscreenPageLimit(int limit)

Set the number of pages that should be retained to either side of the currently visible page(s).

void setOrientation(int orientation)

Sets the orientation of the ViewPager2.

void setPageTransformer(ViewPager2.PageTransformer transformer)

Sets a ViewPager2.PageTransformer that will be called for each attached page whenever the scroll position is changed.

void setUserInputEnabled(boolean enabled)

Enable or disable user initiated scrolling.

void unregisterOnPageChangeCallback(ViewPager2.OnPageChangeCallback callback)

Remove a callback that was previously added via registerOnPageChangeCallback(OnPageChangeCallback).

Protected methods

void dispatchRestoreInstanceState(SparseArray<Parcelable> container)
void onLayout(boolean changed, int l, int t, int r, int b)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods