ViewPager


public class ViewPager extends ViewGroup

Known direct subclasses
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)
@Target(value = ElementType.TYPE)
@Inherited
public annotation ViewPager.DecorView

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.

Callback interface for responding to adapter changes.

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

public interface ViewPager.PageTransformer

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

public class ViewPager.SavedState extends AbsSavedState

This is the persistent state that is saved by ViewPager.

Simple implementation of the OnPageChangeListener interface with stub implementations of each method.

Constants

static final int

Indicates that the pager is currently being dragged by the user.

static final int

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

static final int

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

Public constructors

Public methods

void
addFocusables(ArrayList<View> views, int direction, int focusableMode)

We only want the current page that is being shown to be focusable.

void

Add a listener that will be invoked whenever the adapter for this ViewPager changes.

void

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

void

We only want the current page that is being shown to be touchable.

void
addView(View child, int index, ViewGroup.LayoutParams params)
boolean
arrowScroll(int direction)

Handle scrolling in response to a left or right arrow click.

boolean

Start a fake drag of the pager.

boolean
canScrollHorizontally(int direction)

Check if this ViewPager can be scrolled horizontally in a certain direction.

void

Remove all listeners that are notified of any changes in scroll state or position.

void
boolean
boolean
void
draw(@NonNull Canvas canvas)
void

End a fake drag of the pager.

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.

void
fakeDragBy(float xOffset)

Fake drag by an offset in pixels.

ViewGroup.LayoutParams
@Nullable 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
void
@NonNull Parcelable
boolean
void

Remove a listener that was previously added via addOnAdapterChangeListener.

void

Remove a listener that was previously added via addOnPageChangeListener.

void
void

Set a PagerAdapter that will supply views for this pager as needed.

void
setCurrentItem(int item)

Set the currently selected page.

void
setCurrentItem(int item, boolean smoothScroll)

Set the currently selected page.

void
setDragInGutterEnabled(boolean enabled)

Set whether ViewPager should consume drag events if they are within the gutter (left and right edges) of the ViewPager.

void

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

void

This method is deprecated.

Use addOnPageChangeListener and removeOnPageChangeListener instead.

void
setPageMargin(int marginPixels)

Set the margin between pages.

void

Set a drawable that will be used to fill the margin between pages.

void

Set a drawable that will be used to fill the margin between pages.

void
setPageTransformer(
    boolean reverseDrawingOrder,
    @Nullable ViewPager.PageTransformer transformer
)

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

void
setPageTransformer(
    boolean reverseDrawingOrder,
    @Nullable ViewPager.PageTransformer transformer,
    int pageLayerType
)

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

Protected methods

boolean
canScroll(@NonNull View v, boolean checkV, int dx, int x, int y)

Tests scrollability within child views of v given a delta of dx.

boolean
void
ViewGroup.LayoutParams
ViewGroup.LayoutParams
int
getChildDrawingOrder(int childCount, int i)
void
void
void
void
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void
@CallSuper
onPageScrolled(int position, float offset, int offsetPixels)

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.

boolean
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)

We only want the current page that is being shown to be focusable.

void
onSizeChanged(int w, int h, int oldw, int oldh)
boolean

Inherited Constants

From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
From android.view.ViewGroup
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

Inherited methods

From android.view.View
void
void
void
ViewPropertyAnimator
void
void
boolean
void
void

This method is deprecated.

void
boolean
boolean
boolean
boolean
boolean
canScrollVertically(int direction)
final void
void
final void
boolean
void
void
static int
combineMeasuredStates(int curState, int newState)
int
int
int
WindowInsets
int
int
int
AccessibilityNodeInfo
void
void

This method is deprecated.

boolean
boolean
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean
dispatchNestedPreFling(float velocityX, float velocityY)
boolean
dispatchNestedPrePerformAccessibilityAction(
    int action,
    Bundle arguments
)
boolean
dispatchNestedPreScroll(
    int dx,
    int dy,
    int[] consumed,
    int[] offsetInWindow
)
boolean
dispatchNestedScroll(
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int[] offsetInWindow
)
void
drawableHotspotChanged(float x, float y)
final OnBackInvokedDispatcher
final T
<T extends View> findViewById(int id)
final T
<T extends View> findViewWithTag(Object tag)
boolean

This method is deprecated.

void
forceHasOverlappingRendering(boolean hasOverlappingRendering)
void
void
generateDisplayHash(
    String hashAlgorithm,
    Rect bounds,
    Executor executor,
    DisplayHashResultCallback callback
)
static int
View.AccessibilityDelegate
int
AccessibilityNodeProvider
CharSequence
int
int
String
String
float
Animation
Matrix
IBinder
int[]
Map<IntegerInteger>
String[]
final AutofillId
int
AutofillValue
Drawable
BlendMode
ColorStateList
PorterDuff.Mode
int
final int
float
int
float
Rect
boolean
final boolean
final ContentCaptureSession
CharSequence
final Context
ContextMenu.ContextMenuInfo
final boolean
static int
getDefaultSize(int size, int measureSpec)
Display
final int[]
Bitmap

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void
long
float
int
boolean
boolean
int
ArrayList<View>
getFocusables(int direction)
void
Drawable
int
BlendMode
ColorStateList
PorterDuff.Mode
boolean
getGlobalVisibleRect(Rect r, Point globalOffset)
Handler
float
float
float
float
Runnable
final boolean
final int
void
getHitRect(Rect outRect)
int
int
Drawable
Drawable
int
int
int
int
boolean
KeyEvent.DispatcherState
int
int
int
ViewGroup.LayoutParams
final int
float
int
final boolean
void
getLocationInSurface(int[] location)
void
getLocationInWindow(int[] outLocation)
void
getLocationOnScreen(int[] outLocation)
Matrix
final int
final int
final int
final int
final int
int
int
int