ViewPager2

public final class ViewPager2
extends ViewGroup

java.lang.Object
   ↳ 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:

Summary

Nested classes

class ViewPager2.OnPageChangeCallback

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

@interface ViewPager2.Orientation

 

interface ViewPager2.PageTransformer

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

@interface ViewPager2.ScrollState

 

Constants

int ORIENTATION_HORIZONTAL

int ORIENTATION_VERTICAL

int SCROLL_STATE_DRAGGING

int SCROLL_STATE_IDLE

int SCROLL_STATE_SETTLING

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

boolean beginFakeDrag()

Start a fake drag of the pager.

boolean endFakeDrag()

End a fake drag of the pager.

boolean fakeDragBy(float offsetPxFloat)

Fake drag by an offset in pixels.

Adapter getAdapter()
int getCurrentItem()

Returns the currently selected page.

int getOrientation()
int getScrollState()

Returns the current scroll state of the ViewPager2.

boolean isFakeDragging()

Returns true if a fake drag is in progress.

boolean isUserInputEnabled()

Returns if user initiated scrolling between pages is enabled.

void onViewAdded(View child)
void registerOnPageChangeCallback(ViewPager2.OnPageChangeCallback callback)

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

void setAdapter(Adapter adapter)
void setCurrentItem(int item, boolean smoothScroll)

Set the currently selected page.

void setCurrentItem(int item)

Set the currently selected page.

void setOrientation(int orientation)
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

Constants

ORIENTATION_HORIZONTAL

public static final int ORIENTATION_HORIZONTAL

Constant Value: 0 (0x00000000)

ORIENTATION_VERTICAL

public static final int ORIENTATION_VERTICAL

Constant Value: 1 (0x00000001)

SCROLL_STATE_DRAGGING

public static final int SCROLL_STATE_DRAGGING

Constant Value: 1 (0x00000001)

SCROLL_STATE_IDLE

public static final int SCROLL_STATE_IDLE

Constant Value: 0 (0x00000000)

SCROLL_STATE_SETTLING

public static final int SCROLL_STATE_SETTLING

Constant Value: 2 (0x00000002)

Public constructors

ViewPager2

public ViewPager2 (Context context)

Parameters
context Context

ViewPager2

public ViewPager2 (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

ViewPager2

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

ViewPager2

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

beginFakeDrag

public boolean beginFakeDrag ()

Start a fake drag of the pager.

A fake drag can be useful if you want to synchronize the motion of the ViewPager2 with the touch scrolling of another view, while still letting the ViewPager2 control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) Call fakeDragBy(float) to simulate the actual drag motion. Call endFakeDrag() to complete the fake drag and fling as necessary.

A fake drag can be interrupted by a real drag. From that point on, all calls to fakeDragBy and endFakeDrag will be ignored until the next fake drag is started by calling beginFakeDrag. If you need the ViewPager2 to ignore touch events and other user input during a fake drag, use setUserInputEnabled(boolean). If a real or fake drag is already in progress, this method will return false.

Returns
boolean true if the fake drag began successfully, false if it could not be started

endFakeDrag

public boolean endFakeDrag ()

End a fake drag of the pager.

Returns
boolean true if the fake drag was ended. If false is returned, it means there was no fake drag to end.

fakeDragBy

public boolean fakeDragBy (float offsetPxFloat)

Fake drag by an offset in pixels. You must have called beginFakeDrag() first. Drag happens in the direction of the orientation. Positive offsets will drag to the previous page, negative values to the next page, with one exception: if layout direction is set to RTL and the ViewPager2's orientation is horizontal, then the behavior will be inverted. This matches the deltas of touch events that would cause the same real drag.

If the pager is not in the fake dragging state anymore, it ignores this call and returns false.

Parameters
offsetPxFloat float: Offset in pixels to drag by

Returns
boolean true if the fake drag was executed. If false is returned, it means there was no fake drag to end.

getAdapter

public Adapter getAdapter ()

Returns
Adapter

getCurrentItem

public int getCurrentItem ()

Returns the currently selected page. If no page can sensibly be selected because there is no adapter or the adapter is empty, returns 0.

Returns
int Currently selected page

getOrientation

public int getOrientation ()

Returns
int

getScrollState

public int getScrollState ()

Returns the current scroll state of the ViewPager2. Returned value is one of can be one of SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING or SCROLL_STATE_SETTLING.

Returns
int The scroll state that was last dispatched to ViewPager2.OnPageChangeCallback.onPageScrollStateChanged(int)

isFakeDragging

public boolean isFakeDragging ()

Returns true if a fake drag is in progress.

Returns
boolean true if currently in a fake drag, false otherwise.

isUserInputEnabled

public boolean isUserInputEnabled ()

Returns if user initiated scrolling between pages is enabled. Enabled by default.

Returns
boolean true if users can scroll the ViewPager2, false otherwise

onViewAdded

public void onViewAdded (View child)

Parameters
child View

registerOnPageChangeCallback

public void registerOnPageChangeCallback (ViewPager2.OnPageChangeCallback callback)

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

Components that add a callback should take care to remove it when finished.

Parameters
callback ViewPager2.OnPageChangeCallback: callback to add

setAdapter

public void setAdapter (Adapter adapter)

Parameters
adapter Adapter

setCurrentItem

public void setCurrentItem (int item, 
                boolean smoothScroll)

Set the currently selected page. If smoothScroll = true, will perform a smooth animation from the current item to the new item. Silently ignored if the adapter is not set or empty. Clamps item to the bounds of the adapter.

Parameters
item int: Item index to select

smoothScroll boolean: True to smoothly scroll to the new item, false to transition immediately

setCurrentItem

public void setCurrentItem (int item)

Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item. Silently ignored if the adapter is not set or empty. Clamps item to the bounds of the adapter. TODO(b/123069219): verify first layout behavior

Parameters
item int: Item index to select

setOrientation

public void setOrientation (int orientation)

Parameters
orientation int: ViewPager2.Orientation

setPageTransformer

public void setPageTransformer (ViewPager2.PageTransformer transformer)

Sets a ViewPager2.PageTransformer that will be called for each attached page whenever the scroll position is changed. This allows the application to apply custom property transformations to each page, overriding the default sliding behavior.

Parameters
transformer ViewPager2.PageTransformer: PageTransformer that will modify each page's animation properties

setUserInputEnabled

public void setUserInputEnabled (boolean enabled)

Enable or disable user initiated scrolling. This includes touch input (scroll and fling gestures) and accessibility input. Disabling keyboard input is not yet supported. When user initiated scrolling is disabled, programmatic scrolls through setCurrentItem still work. By default, user initiated scrolling is enabled.

Parameters
enabled boolean: true to allow user initiated scrolling, false to block user initiated scrolling

unregisterOnPageChangeCallback

public void unregisterOnPageChangeCallback (ViewPager2.OnPageChangeCallback callback)

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

Parameters
callback ViewPager2.OnPageChangeCallback: callback to remove

Protected methods

dispatchRestoreInstanceState

protected void dispatchRestoreInstanceState (SparseArray<Parcelable> container)

Parameters
container SparseArray

onLayout

protected void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

Parameters
changed boolean

l int

t int

r int

b int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

protected void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

protected Parcelable onSaveInstanceState ()

Returns
Parcelable