ViewPager2

public 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

final Adapter getAdapter()
final int getCurrentItem()

Returns the currently selected page.

final int getOrientation()
final void onViewAdded(View child)
final void registerOnPageChangeCallback(ViewPager2.OnPageChangeCallback callback)

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

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

Set the currently selected page.

final void setCurrentItem(int item)

Set the currently selected page.

final void setOrientation(int orientation)
final void setPageTransformer(ViewPager2.PageTransformer transformer)

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

final 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

getAdapter

public final Adapter getAdapter ()

Returns
Adapter

getCurrentItem

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

Returns
int

onViewAdded

public final void onViewAdded (View child)

Parameters
child View

registerOnPageChangeCallback

public final 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 final void setAdapter (Adapter adapter)

Parameters
adapter Adapter

setCurrentItem

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

Parameters
orientation int: ViewPager2.Orientation

setPageTransformer

public final void setPageTransformer (ViewPager2.PageTransformer transformer)

Sets a ViewPager.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

unregisterOnPageChangeCallback

public final 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