Google is committed to advancing racial equity for Black communities. See how.

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

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.

static Int

static Int

static Int

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

static Int

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

static Int

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

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
Unit

Add an ItemDecoration to this ViewPager2.

Unit

Add an ItemDecoration to this ViewPager2.

Boolean

Start a fake drag of the pager.

Boolean

Boolean

Boolean

End a fake drag of the pager.

Boolean
fakeDragBy(@Px offsetPxFloat: Float)

Fake drag by an offset in pixels.

CharSequence!

RecyclerView.Adapter<RecyclerView.ViewHolder!>?

Int

Returns the currently selected page.

RecyclerView.ItemDecoration

Returns an ItemDecoration previously added to this ViewPager2.

Int

Returns the number of ItemDecoration currently added to this ViewPager2.

Int

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

Int

Returns the current scroll state of the ViewPager2.

Unit

Invalidates all ItemDecorations.

Boolean

Returns true if a fake drag is in progress.

Boolean

Returns if user initiated scrolling between pages is enabled.

Unit

Unit
onViewAdded(child: View!)

Boolean
performAccessibilityAction(action: Int, arguments: Bundle!)

Unit

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

Unit

Remove an ItemDecoration from this ViewPager2.

Unit

Removes the ItemDecoration associated with the supplied index position.

Unit

Trigger a call to the registered PageTransformer's method.

Unit

Set a new adapter to provide page views on demand.

Unit

Set the currently selected page.

Unit
setCurrentItem(item: Int, smoothScroll: Boolean)

Set the currently selected page.

Unit
setLayoutDirection(layoutDirection: Int)

Unit

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

Unit
setOrientation(orientation: Int)

Sets the orientation of the ViewPager2.

Unit

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

Unit

Enable or disable user initiated scrolling.

Unit

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

Protected methods
Unit

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

Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Unit

Parcelable?

Constants

OFFSCREEN_PAGE_LIMIT_DEFAULT

static val OFFSCREEN_PAGE_LIMIT_DEFAULT: Int

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.

Value: -1

ORIENTATION_HORIZONTAL

static val ORIENTATION_HORIZONTAL: Int
Value: RecyclerView.HORIZONTAL

ORIENTATION_VERTICAL

static val ORIENTATION_VERTICAL: Int
Value: RecyclerView.VERTICAL

SCROLL_STATE_DRAGGING

static val SCROLL_STATE_DRAGGING: Int

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

Value: 1

SCROLL_STATE_IDLE

static val SCROLL_STATE_IDLE: Int

Indicates that the ViewPager2 is in an idle, settled state. The current page is fully in view and no animation is in progress.

Value: 0

SCROLL_STATE_SETTLING

static