Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

ViewPager2

class ViewPager2 : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.viewpager2.widget.ViewPager2

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

Summary

Nested classes

abstract

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

abstract

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

Constants
static Int

static Int

static Int

static Int

static Int

Public constructors
<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)

Public methods
Int

Returns the currently selected page.

Boolean

Returns if user initiated scrolling between pages is enabled.

Unit

Enable or disable user initiated scrolling.

Unit

Boolean

Start a fake drag of the pager.

Unit
onViewAdded(child: View!)

Int

Returns the current scroll state of the ViewPager2.

RecyclerView.Adapter<RecyclerView.ViewHolder!>?

Unit

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

Boolean

Returns true if a fake drag is in progress.

Unit

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

Unit

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

Unit
setOrientation(orientation: Int)

Boolean
fakeDragBy(offsetPxFloat: Float)

Fake drag by an offset in pixels.

Unit

Set the currently selected page.

Unit
setCurrentItem(item: Int, smoothScroll: Boolean)

Set the currently selected page.

Int

Boolean

End a fake drag of the pager.

Protected methods
Unit

Parcelable?

Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Unit

Constants

ORIENTATION_HORIZONTAL

static val ORIENTATION_HORIZONTAL: Int
Value: RecyclerView.HORIZONTAL

ORIENTATION_VERTICAL

static val ORIENTATION_VERTICAL: Int
Value: RecyclerView.VERTICAL

SCROLL_STATE_IDLE

static val SCROLL_STATE_IDLE: Int
Value: 0

SCROLL_STATE_DRAGGING

static val SCROLL_STATE_DRAGGING: Int
Value: 1

SCROLL_STATE_SETTLING

static val SCROLL_STATE_SETTLING: Int
Value: 2

Public constructors

<init>

ViewPager2(@NonNull context: Context)

<init>

ViewPager2(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>

ViewPager2(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

<init>

ViewPager2(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)

Public methods

getCurrentItem

fun getCurrentItem(): Int

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

Return
Int: Currently selected page

isUserInputEnabled

fun isUserInputEnabled(): Boolean

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

Return
Boolean: true if users can scroll the ViewPager2, false otherwise

setUserInputEnabled

fun setUserInputEnabled(enabled: Boolean): Unit

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

beginFakeDrag

fun beginFakeDrag(): Boolean

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.

Return
Boolean: true if the fake drag began successfully, false if it could not be started

onViewAdded

fun onViewAdded(child: View!): Unit

getScrollState

fun getScrollState(): Int

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.

Return
Int: The scroll state that was last dispatched to

getAdapter

@Nullable fun getAdapter(): RecyclerView.Adapter<RecyclerView.ViewHolder!>?

unregisterOnPageChangeCallback

fun unregisterOnPageChangeCallback(@NonNull callback: ViewPager2.OnPageChangeCallback): Unit

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

Parameters
callback ViewPager2.OnPageChangeCallback: callback to remove

isFakeDragging

fun isFakeDragging(): Boolean

Returns true if a fake drag is in progress.

Return
Boolean: true if currently in a fake drag, false otherwise.

setPageTransformer

fun setPageTransformer(@Nullable transformer: ViewPager2.PageTransformer?): Unit

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

registerOnPageChangeCallback

fun registerOnPageChangeCallback(@NonNull callback: ViewPager2.OnPageChangeCallback): Unit

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

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

Parameters
callback ViewPager2.OnPageChangeCallback: callback to add

setOrientation

fun setOrientation(orientation: Int): Unit
Parameters
orientation Int: ViewPager2.Orientation

fakeDragBy

fun fakeDragBy(offsetPxFloat: Float): Boolean

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
Return
Boolean: true if the fake drag was executed. If false is returned, it means there was no fake drag to end.

setCurrentItem

fun setCurrentItem(item: Int): Unit

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

setCurrentItem

fun setCurrentItem(item: Int, smoothScroll: Boolean): Unit

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 Int: True to smoothly scroll to the new item, false to transition immediately

getOrientation

fun getOrientation(): Int

endFakeDrag

fun endFakeDrag(): Boolean

End a fake drag of the pager.

Return
Boolean: true if the fake drag was ended. If false is returned, it means there was no fake drag to end.

Protected methods

dispatchRestoreInstanceState

protected fun dispatchRestoreInstanceState(container: SparseArray<Parcelable!>!): Unit

onSaveInstanceState

@Nullable protected fun onSaveInstanceState(): Parcelable?

onLayout

protected fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int): Unit

onMeasure

protected fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

onRestoreInstanceState

protected fun onRestoreInstanceState(state: Parcelable!): Unit