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.

A note on WindowInsets: due to a bug in API 29 and before, WindowInsets were incorrectly dispatched to a View's children and siblings. ViewPager2 offers a fix to make sure that all pages receive the correct insets, but it comes at the cost of not dispatching insets to any siblings of ViewPager2 or siblings of its ancestors that haven't received the insets yet. If you want to handle insets on each page separately (instead of on ViewPager2 or one of its parents), you should install a WindowInsetsApplier by calling WindowInsetsApplier.install(viewPager2). If you don't handle insets on any of the pages, we recommend you don't use the WindowInsetsApplier.

See also

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)
@RequiresApi(value = 21)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int)

Types

Link copied to clipboard
annotation class OffscreenPageLimit
Link copied to clipboard
abstract class OnPageChangeCallback
Callback interface for responding to changing state of the selected page.
Link copied to clipboard
annotation class Orientation
Link copied to clipboard
interface PageTransformer
A PageTransformer is invoked whenever a visible/attached page is scrolled.
Link copied to clipboard
annotation class ScrollState

Properties

Link copied to clipboard
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.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Indicates that the ViewPager2 is currently being dragged by the user, or programmatically via fake drag functionality.
Link copied to clipboard
Indicates that the ViewPager2 is in an idle, settled state.
Link copied to clipboard
Indicates that the ViewPager2 is in the process of settling to a final position.

Functions

Link copied to clipboard
Link copied to clipboard
Start a fake drag of the pager.
Link copied to clipboard
open fun canScrollHorizontally(direction: Int): Boolean
Link copied to clipboard
open fun canScrollVertically(direction: Int): Boolean
Link copied to clipboard
open fun endFakeDrag(): Boolean
End a fake drag of the pager.
Link copied to clipboard
open fun fakeDragBy(@Px offsetPxFloat: Float): Boolean
Fake drag by an offset in pixels.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun getCurrentItem(): Int
Returns the currently selected page.
Link copied to clipboard
Returns an ItemDecoration previously added to this ViewPager2.
Link copied to clipboard
Returns the number of ItemDecoration currently added to this ViewPager2.
Link copied to clipboard
Returns the number of pages that will be retained to either side of the current page in the view hierarchy in an idle state.
Link copied to clipboard
open fun getOrientation(): Int
Link copied to clipboard
open fun getScrollState(): Int
Returns the current scroll state of the ViewPager2.
Link copied to clipboard
Invalidates all ItemDecorations.
Link copied to clipboard
Returns true if a fake drag is in progress.
Link copied to clipboard
Returns if user initiated scrolling between pages is enabled.
Link copied to clipboard
open fun onViewAdded(child: View)
Link copied to clipboard
open fun performAccessibilityAction(action: Int, @Nullable arguments: Bundle): Boolean
Link copied to clipboard
Add a callback that will be invoked whenever the page changes or is incrementally scrolled.
Link copied to clipboard
Remove an ItemDecoration from this ViewPager2.
Link copied to clipboard
open fun removeItemDecorationAt(index: Int)
Removes the ItemDecoration associated with the supplied index position.
Link copied to clipboard
open fun requestTransform()
Trigger a call to the registered PageTransformer's transformPage method.
Link copied to clipboard
open fun seslSetSuggestionPaging(suggestionPagingEnabled: Boolean)
Link copied to clipboard
Set a new adapter to provide page views on demand.
Link copied to clipboard
open fun setCurrentItem(item: Int)
open fun setCurrentItem(item: Int, smoothScroll: Boolean)
Set the currently selected page.
Link copied to clipboard
open fun setLayoutDirection(layoutDirection: Int)
Link copied to clipboard
open fun setOffscreenPageLimit(limit: Int)
Set the number of pages that should be retained to either side of the currently visible page(s).
Link copied to clipboard
open fun setOrientation(orientation: Int)
Sets the orientation of the ViewPager2.
Link copied to clipboard
Sets a PageTransformer that will be called for each attached page whenever the scroll position is changed.
Link copied to clipboard
open fun setUserInputEnabled(enabled: Boolean)
Enable or disable user initiated scrolling.
Link copied to clipboard
Remove a callback that was previously added via registerOnPageChangeCallback.