ViewPager

open class ViewPager : ViewGroup(source)

(SESL variant) Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

ViewPager is most often used in conjunction with android.app.Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are androidx.fragment.app.FragmentPagerAdapter and androidx.fragment.app.FragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.

Views which are annotated with the DecorView annotation are treated as part of the view pagers 'decor'. Each decor view's position can be controlled via its android:layout_gravity attribute. For example:

<androidx.viewpager.widget.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager.widget.PagerTitleStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</androidx.viewpager.widget.ViewPager>

For more information about how to use ViewPager, read Creating Swipe Views with Tabs.

You can find examples of using ViewPager in the API 4+ Support Demos and API 13+ Support Demos sample code.

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)

Types

Link copied to clipboard
Annotation which allows marking of views to be decoration views when added to a view pager.
Link copied to clipboard
Layout parameters that should be supplied for views added to a ViewPager.
Link copied to clipboard
Callback interface for responding to adapter changes.
Link copied to clipboard
Callback interface for responding to changing state of the selected page.
Link copied to clipboard
interface PageTransformer
A PageTransformer is invoked whenever a visible/attached page is scrolled.
Link copied to clipboard
This is the persistent state that is saved by ViewPager.
Link copied to clipboard
Simple implementation of the OnPageChangeListener interface with stub implementations of each method.

Properties

Link copied to clipboard
Indicates that the pager is currently being dragged by the user.
Link copied to clipboard
Indicates that the pager is in an idle, settled state.
Link copied to clipboard
Indicates that the pager is in the process of settling to a final position.

Functions

Link copied to clipboard
open fun addFocusables(views: ArrayList<View>, direction: Int, focusableMode: Int)
We only want the current page that is being shown to be focusable.
Link copied to clipboard
Add a listener that will be invoked whenever the adapter for this ViewPager changes.
Link copied to clipboard
Add a listener that will be invoked whenever the page changes or is incrementally scrolled.
Link copied to clipboard
open fun addTouchables(views: ArrayList<View>)
We only want the current page that is being shown to be touchable.
Link copied to clipboard
open fun addView(child: View, index: Int, params: ViewGroup.LayoutParams)
Link copied to clipboard
open fun arrowScroll(direction: Int): Boolean
Handle scrolling in response to a left or right arrow click.
Link copied to clipboard
Start a fake drag of the pager.
Link copied to clipboard
open fun canScrollHorizontally(direction: Int): Boolean
Check if this ViewPager can be scrolled horizontally in a certain direction.
Link copied to clipboard
Remove all listeners that are notified of any changes in scroll state or position.
Link copied to clipboard
open fun computeScroll()
Link copied to clipboard
Link copied to clipboard
open fun draw(@NonNull canvas: Canvas)
Link copied to clipboard
open fun endFakeDrag()
End a fake drag of the pager.
Link copied to clipboard
You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.
Link copied to clipboard
open fun fakeDragBy(xOffset: Float)
Fake drag by an offset in pixels.
Link copied to clipboard
Retrieve the current adapter supplying pages.
Link copied to clipboard
open fun getCurrentItem(): Int
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 getPageMargin(): Int
Return the margin between pages.
Link copied to clipboard
Link copied to clipboard
Returns true if a fake drag is in progress.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun onRtlPropertiesChanged(layoutDirection: Int)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Remove a listener that was previously added via addOnAdapterChangeListener.
Link copied to clipboard
Remove a listener that was previously added via addOnPageChangeListener.
Link copied to clipboard
open fun removeView(view: View)
Link copied to clipboard
Link copied to clipboard
open fun seslGetTouchSlop(): Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun seslSetTouchSlop(slopConstant: Int)
Link copied to clipboard
open fun seslSetTouchSlopRatio(ratio: Float)
Link copied to clipboard
open fun setAdapter(@Nullable adapter: PagerAdapter)
Set a PagerAdapter that will supply views for this pager as needed.
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 setDragInGutterEnabled(enabled: Boolean)
Set whether ViewPager should consume drag events if they are within the gutter (left and right edges) of the ViewPager.
Link copied to clipboard
open fun setOffscreenPageLimit(limit: Int)
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
Link copied to clipboard
Set a listener that will be invoked whenever the page changes or is incrementally scrolled.
Link copied to clipboard
open fun setPageMargin(marginPixels: Int)
Set the margin between pages.
Link copied to clipboard
Set a drawable that will be used to fill the margin between pages.
Link copied to clipboard
open fun setPageTransformer(reverseDrawingOrder: Boolean, @Nullable transformer: ViewPager.PageTransformer)
open fun setPageTransformer(reverseDrawingOrder: Boolean, @Nullable transformer: ViewPager.PageTransformer, pageLayerType: Int)
Sets a PageTransformer that will be called for each attached page whenever the scroll position is changed.