ViewPager
class ViewPager : ViewGroup
LeanbackViewPager |
A viewpager with touch and key event handling disabled by default. |
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.
Summary
Nested types |
|---|
@Retention(value = RetentionPolicy.RUNTIME)Annotation which allows marking of views to be decoration views when added to a view pager. |
|
Layout parameters that should be supplied for views added to a ViewPager. |
interface ViewPager.OnAdapterChangeListenerCallback interface for responding to adapter changes. |
interface ViewPager.OnPageChangeListenerCallback interface for responding to changing state of the selected page. |
interface ViewPager.PageTransformerA PageTransformer is invoked whenever a visible/attached page is scrolled. |
class ViewPager.SavedState : AbsSavedStateThis is the persistent state that is saved by ViewPager. |
|
Simple implementation of the |
Constants |
|
|---|---|
const Int |
Indicates that the pager is currently being dragged by the user. |
const Int |
Indicates that the pager is in an idle, settled state. |
const Int |
Indicates that the pager is in the process of settling to a final position. |
Public constructors |
|---|
ViewPager(context: Context, attrs: AttributeSet?) |
Public functions |
|
|---|---|
Unit |
addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int)We only want the current page that is being shown to be focusable. |
Unit |
Add a listener that will be invoked whenever the adapter for this ViewPager changes. |
Unit |
Add a listener that will be invoked whenever the page changes or is incrementally scrolled. |
Unit |
addTouchables(views: ArrayList<View!>!)We only want the current page that is being shown to be touchable. |
Unit |
addView(child: View!, index: Int, params: ViewGroup.LayoutParams!) |
Boolean |
arrowScroll(direction: Int)Handle scrolling in response to a left or right arrow click. |
Boolean |
Start a fake drag of the pager. |
Boolean |
canScrollHorizontally(direction: Int)Check if this ViewPager can be scrolled horizontally in a certain direction. |
Unit |
Remove all listeners that are notified of any changes in scroll state or position. |
Unit |
|
Boolean |
dispatchKeyEvent(event: KeyEvent!) |
Boolean |
|
Unit |
|
Unit |
End a fake drag of the pager. |
Boolean |
executeKeyEvent(event: KeyEvent)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. |
Unit |
fakeDragBy(xOffset: Float)Fake drag by an offset in pixels. |
ViewGroup.LayoutParams! |
generateLayoutParams(attrs: AttributeSet!) |
PagerAdapter? |
Retrieve the current adapter supplying pages. |
Int |
|
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 |
Return the margin between pages. |
Boolean |
|
Boolean |
Returns true if a fake drag is in progress. |
Boolean |
|
Unit |
onRestoreInstanceState(state: Parcelable!) |
Parcelable |
|
Boolean |
onTouchEvent(ev: MotionEvent!) |
Unit |
Remove a listener that was previously added via |
Unit |
Remove a listener that was previously added via |
Unit |
removeView(view: View!) |
Unit |
setAdapter(adapter: PagerAdapter?)Set a PagerAdapter that will supply views for this pager as needed. |
Unit |
setCurrentItem(item: Int)Set the currently selected page. |
Unit |
setCurrentItem(item: Int, smoothScroll: Boolean)Set the currently selected page. |
Unit |
setDragInGutterEnabled(enabled: Boolean)Set whether ViewPager should consume drag events if they are within the gutter (left and right edges) of the ViewPager. |
Unit |
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. |
Unit |
This function is deprecated. Use |
Unit |
setPageMargin(marginPixels: Int)Set the margin between pages. |
Unit |
Set a drawable that will be used to fill the margin between pages. |
Unit |
setPageMarginDrawable(resId: @DrawableRes Int)Set a drawable that will be used to fill the margin between pages. |
Unit |
setPageTransformer(Sets a |
Unit |
setPageTransformer(Sets a |
Protected functions |
|
|---|---|
Boolean |
Tests scrollability within child views of v given a delta of dx. |
Boolean |
|
Unit |
|
ViewGroup.LayoutParams! |
|
ViewGroup.LayoutParams! |
|
Int |
getChildDrawingOrder(childCount: Int, i: Int) |
Unit |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
@CallSuperThis method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll. |
Boolean |
onRequestFocusInDescendants(direction: Int, previouslyFocusedRect: Rect!)We only want the current page that is being shown to be focusable. |
Unit |
onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) |
Boolean |
verifyDrawable(who: Drawable) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
SCROLL_STATE_DRAGGING
const val SCROLL_STATE_DRAGGING = 1: Int
Indicates that the pager is currently being dragged by the user.
SCROLL_STATE_IDLE
const val SCROLL_STATE_IDLE = 0: Int
Indicates that the pager is in an idle, settled state. The current page is fully in view and no animation is in progress.
SCROLL_STATE_SETTLING
const val SCROLL_STATE_SETTLING = 2: Int
Indicates that the pager is in the process of settling to a final position.
Public constructors
Public functions
addFocusables
fun addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int): Unit
We only want the current page that is being shown to be focusable.
addOnAdapterChangeListener
fun addOnAdapterChangeListener(listener: ViewPager.OnAdapterChangeListener): Unit
Add a listener that will be invoked whenever the adapter for this ViewPager changes.
| Parameters | |
|---|---|
listener: ViewPager.OnAdapterChangeListener |
listener to add |
addOnPageChangeListener
fun addOnPageChangeListener(listener: ViewPager.OnPageChangeListener): Unit
Add a listener that will be invoked whenever the page changes or is incrementally scrolled. See OnPageChangeListener.
Components that add a listener should take care to remove it when finished. Other components that take ownership of a view may call clearOnPageChangeListeners to remove all attached listeners.
| Parameters | |
|---|---|
listener: ViewPager.OnPageChangeListener |
listener to add |
addTouchables
fun addTouchables(views: ArrayList<View!>!): Unit
We only want the current page that is being shown to be touchable.
arrowScroll
fun arrowScroll(direction: Int): Boolean
Handle scrolling in response to a left or right arrow click.
| Parameters | |
|---|---|
direction: Int |
The direction corresponding to the arrow key that was pressed. It should be either |
| Returns | |
|---|---|
Boolean |
Whether the scrolling was handled successfully. |
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 ViewPager with the touch scrolling of another view, while still letting the ViewPager control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) Call fakeDragBy to simulate the actual drag motion. Call endFakeDrag to complete the fake drag and fling as necessary.
During a fake drag the ViewPager will ignore all touch events. If a real drag is already in progress, this method will return false.
| Returns | |
|---|---|
Boolean |
true if the fake drag began successfully, false if it could not be started. |
| See also | |
|---|---|
fakeDragBy |
|
endFakeDrag |
canScrollHorizontally
fun canScrollHorizontally(direction: Int): Boolean
Check if this ViewPager can be scrolled horizontally in a certain direction.
| Parameters | |
|---|---|
direction: Int |
Negative to check scrolling left, positive to check scrolling right. |
| Returns | |
|---|---|
Boolean |
Whether this ViewPager can be scrolled in the specified direction. It will always return false if the specified direction is 0. |
clearOnPageChangeListeners
fun clearOnPageChangeListeners(): Unit
Remove all listeners that are notified of any changes in scroll state or position.
dispatchPopulateAccessibilityEvent
fun dispatchPopulateAccessibilityEvent(event: AccessibilityEvent!): Boolean
endFakeDrag
fun endFakeDrag(): Unit
End a fake drag of the pager.
| See also | |
|---|---|
beginFakeDrag |
|
fakeDragBy |
executeKeyEvent
fun executeKeyEvent(event: KeyEvent): Boolean
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.
| Parameters | |
|---|---|
event: KeyEvent |
The key event to execute. |
| Returns | |
|---|---|
Boolean |
Return true if the event was handled, else false. |
fakeDragBy
fun fakeDragBy(xOffset: Float): Unit
Fake drag by an offset in pixels. You must have called beginFakeDrag first.
| Parameters | |
|---|---|
xOffset: Float |
Offset in pixels to drag by. |
| See also | |
|---|---|
beginFakeDrag |
|
endFakeDrag |
getAdapter
fun getAdapter(): PagerAdapter?
Retrieve the current adapter supplying pages.
| Returns | |
|---|---|
PagerAdapter? |
The currently registered PagerAdapter |
getOffscreenPageLimit
fun getOffscreenPageLimit(): 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. Defaults to 1.
| Returns | |
|---|---|
Int |
How many pages will be kept offscreen on either side |
| See also | |
|---|---|
setOffscreenPageLimit |
getPageMargin
fun getPageMargin(): Int
Return the margin between pages.
| Returns | |
|---|---|
Int |
The size of the margin in pixels |
isDragInGutterEnabled
fun isDragInGutterEnabled(): Boolean
| Returns | |
|---|---|
Boolean |
Whether dragging in the gutter (left and right edges) of the ViewPager is enabled. |
isFakeDragging
fun isFakeDragging(): Boolean
Returns true if a fake drag is in progress.
| Returns | |
|---|---|
Boolean |
true if currently in a fake drag, false otherwise. |
| See also | |
|---|---|
beginFakeDrag |
|
fakeDragBy |
|
endFakeDrag |
removeOnAdapterChangeListener
fun removeOnAdapterChangeListener(
listener: ViewPager.OnAdapterChangeListener
): Unit
Remove a listener that was previously added via addOnAdapterChangeListener.
| Parameters | |
|---|---|
listener: ViewPager.OnAdapterChangeListener |
listener to remove |
removeOnPageChangeListener
fun removeOnPageChangeListener(listener: ViewPager.OnPageChangeListener): Unit
Remove a listener that was previously added via addOnPageChangeListener.
| Parameters | |
|---|---|
listener: ViewPager.OnPageChangeListener |
listener to remove |
setAdapter
fun setAdapter(adapter: PagerAdapter?): Unit
Set a PagerAdapter that will supply views for this pager as needed.
| Parameters | |
|---|---|
adapter: PagerAdapter? |
Adapter to use |
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.
| Parameters | |
|---|---|
item: Int |
Item index to select |
setCurrentItem
fun setCurrentItem(item: Int, smoothScroll: Boolean): Unit
Set the currently selected page.
setDragInGutterEnabled
fun setDragInGutterEnabled(enabled: Boolean): Unit
Set whether ViewPager should consume drag events if they are within the gutter (left and right edges) of the ViewPager. The default value false.
| Parameters | |
|---|---|
enabled: Boolean |
true if ViewPager should allow drag in gutter, false otherwise |
setOffscreenPageLimit
fun setOffscreenPageLimit(limit: Int): Unit
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.
| Parameters | |
|---|---|
limit: Int |
How many pages will be kept offscreen in an idle state. |
funsetOnPageChangeListener(listener: ViewPager.OnPageChangeListener!): Unit
Set a listener that will be invoked whenever the page changes or is incrementally scrolled. See OnPageChangeListener.
| Parameters | |
|---|---|
listener: ViewPager.OnPageChangeListener! |
Listener to set |
setPageMargin
fun setPageMargin(marginPixels: Int): Unit
Set the margin between pages.
| Parameters | |
|---|---|
marginPixels: Int |
Distance between adjacent pages in pixels |
setPageMarginDrawable
fun setPageMarginDrawable(d: Drawable?): Unit
Set a drawable that will be used to fill the margin between pages.
| Parameters | |
|---|---|
d: Drawable? |
Drawable to display between pages |
setPageMarginDrawable
fun setPageMarginDrawable(resId: @DrawableRes Int): Unit
Set a drawable that will be used to fill the margin between pages.
| Parameters | |
|---|---|
resId: @DrawableRes Int |
Resource ID of a drawable to display between pages |
setPageTransformer
fun setPageTransformer(
reverseDrawingOrder: Boolean,
transformer: ViewPager.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.
Note: By default, calling this method will cause contained pages to use LAYER_TYPE_HARDWARE. This layer type allows custom alpha transformations, but it will cause issues if any of your pages contain a android.view.SurfaceView and you have not called setZOrderOnTop to put that android.view.SurfaceView above your app content. To disable this behavior, call setPageTransformer and pass LAYER_TYPE_NONE for pageLayerType.
| Parameters | |
|---|---|
reverseDrawingOrder: Boolean |
true if the supplied PageTransformer requires page views to be drawn from last to first instead of first to last. |
transformer: ViewPager.PageTransformer? |
PageTransformer that will modify each page's animation properties |
setPageTransformer
fun setPageTransformer(
reverseDrawingOrder: Boolean,
transformer: ViewPager.PageTransformer?,
pageLayerType: Int
): 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 | |
|---|---|
reverseDrawingOrder: Boolean |
true if the supplied PageTransformer requires page views to be drawn from last to first instead of first to last. |
transformer: ViewPager.PageTransformer? |
PageTransformer that will modify each page's animation properties |
pageLayerType: Int |
View layer type that should be used for ViewPager pages. It should be either |
Protected functions
canScroll
protected fun canScroll(v: View, checkV: Boolean, dx: Int, x: Int, y: Int): Boolean
Tests scrollability within child views of v given a delta of dx.
| Parameters | |
|---|---|
v: View |
View to test for horizontal scrollability |
checkV: Boolean |
Whether the view v passed should itself be checked for scrollability (true), or just its children (false). |
dx: Int |
Delta scrolled in pixels |
x: Int |
X coordinate of the active touch point |
y: Int |
Y coordinate of the active touch point |
| Returns | |
|---|---|
Boolean |
true if child views of v can be scrolled by delta of dx. |
generateLayoutParams
protected fun generateLayoutParams(p: ViewGroup.LayoutParams!): ViewGroup.LayoutParams!
onPageScrolled
@CallSuper
protected fun onPageScrolled(position: Int, offset: Float, offsetPixels: Int): Unit
This method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll. If you override this method you must call through to the superclass implementation (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled returns.
onRequestFocusInDescendants
protected fun onRequestFocusInDescendants(direction: Int, previouslyFocusedRect: Rect!): Boolean
We only want the current page that is being shown to be focusable.