Google 致力于为黑人社区推动种族平等。查看具体举措

ViewPager

open class ViewPager : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.viewpager.widget.ViewPager

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 classes

Annotation which allows marking of views to be decoration views when added to a view pager.

open

Layout parameters that should be supplied for views added to a ViewPager.

abstract

Callback interface for responding to adapter changes.

abstract

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

abstract

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

open

This is the persistent state that is saved by ViewPager.

open

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

Constants
static Int

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

static Int

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

static Int

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

Public constructors
<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

Public methods
open Unit
addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int)

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

open Unit

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

open Unit

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

open Unit

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

open Unit
addView(child: View!, index: Int, params: LayoutParams!)

open Boolean
arrowScroll(direction: Int)

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

open Boolean

Start a fake drag of the pager.

open Boolean

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

open Unit

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

open Unit

open Boolean

open Boolean

open Unit
draw(canvas: Canvas!)

open Unit

End a fake drag of the pager.

open Boolean
executeKeyEvent(@NonNull 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.

open Unit
fakeDragBy(xOffset: Float)

Fake drag by an offset in pixels.

open LayoutParams!

open PagerAdapter?

Retrieve the current adapter supplying pages.

open Int

open 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.

open Int

Return the margin between pages.

open Boolean

Returns true if a fake drag is in progress.

open Boolean

open Unit

open Parcelable?

open Boolean

open Unit

Remove a listener that was previously added via addOnAdapterChangeListener(OnAdapterChangeListener).

open Unit

Remove a listener that was previously added via addOnPageChangeListener(OnPageChangeListener).

open Unit
removeView(view: View!)

open Unit
setAdapter(@Nullable adapter: PagerAdapter?)

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

open Unit

Set the currently selected page.

open Unit
setCurrentItem(item: Int, smoothScroll: Boolean)

Set the currently selected page.

open 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.

open Unit

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

open Unit
setPageMargin(marginPixels: Int)

Set the margin between pages.

open Unit

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

open Unit
setPageMarginDrawable(@DrawableRes resId: Int)

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

open Unit
setPageTransformer(reverseDrawingOrder: Boolean, @Nullable transformer: ViewPager.PageTransformer?)

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

open Unit
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.

Protected methods
open Boolean
canScroll(v: View!, checkV: Boolean, dx: Int, x: Int, y: Int)

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

open Boolean

open Unit

open LayoutParams!

open LayoutParams!

open Int
getChildDrawingOrder(childCount: Int, i: Int)

open Unit

open Unit

open Unit
onDraw(canvas: Canvas!)

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

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Unit
onPageScrolled(position: Int, offset: Float, offsetPixels: Int)

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.

open Boolean
onRequestFocusInDescendants(direction: Int, previouslyFocusedRect: Rect!)

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

open Unit
onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)

open Boolean

Constants

SCROLL_STATE_DRAGGING

static val SCROLL_STATE_DRAGGING: Int

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

Value: 1

SCROLL_STATE_IDLE

static val SCROLL_STATE_IDLE: Int

Indicates that the pager 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 val SCROLL_STATE_SETTLING: Int

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

Value: 2

Public constructors

<init>

ViewPager(@NonNull context: Context)

<init>

ViewPager(
    @NonNull context: Context,
    @Nullable attrs: AttributeSet?)

Public methods

addFocusables

open fun addFocusables(
    views: ArrayList<View!>!,
    direction: Int,
    focusableMode: Int
): Unit

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

addOnAdapterChangeListener

open fun addOnAdapterChangeListener(@NonNull 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

open fun addOnPageChangeListener(@NonNull 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

open fun addTouchables(views: ArrayList<View!>!): Unit

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

addView

open fun addView(
    child: View!,
    index: Int,
    params: LayoutParams!
): Unit

arrowScroll

open 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