Class ViewPager
- All Implemented Interfaces:
Drawable.Callback
,ViewManager
,ViewParent
You supply an implementation of a
PagerAdapter
to generate the pages that the view shows.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic @interface
Annotation which allows marking of views to be decoration views when added to a view pager.static class
Layout parameters that should be supplied for views added to a ViewPager.static interface
Callback interface for responding to adapter changes.static interface
Callback interface for responding to changing state of the selected page.static interface
A PageTransformer is invoked whenever a visible/attached page is scrolled.Nested classes/interfaces inherited from class icyllis.modernui.view.ViewGroup
ViewGroup.MarginLayoutParams
Nested classes/interfaces inherited from class icyllis.modernui.view.View
View.DragShadow, View.Focusable, View.FocusableMode, View.FocusDirection, View.FocusRealDirection, View.LayoutDir, View.NestedScrollType, View.OnAttachStateChangeListener, View.OnClickListener, View.OnContextClickListener, View.OnCreateContextMenuListener, View.OnDragListener, View.OnFocusChangeListener, View.OnGenericMotionListener, View.OnHoverListener, View.OnKeyListener, View.OnLayoutChangeListener, View.OnLongClickListener, View.OnScrollChangeListener, View.OnTouchListener, View.ResolvedLayoutDir, View.ScrollAxis, View.ScrollBarStyle, View.ScrollIndicators, View.TextAlignment, View.Visibility
-
Field Summary
Modifier and TypeFieldDescriptionstatic 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.Fields inherited from class icyllis.modernui.view.ViewGroup
CLIP_TO_PADDING_MASK, FLAG_DISALLOW_INTERCEPT, FOCUS_AFTER_DESCENDANTS, FOCUS_BEFORE_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS
Fields inherited from class icyllis.modernui.view.View
ALPHA, BOTTOM, EMPTY_STATE_SET, ENABLED_FOCUSED_SELECTED_STATE_SET, ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, ENABLED_FOCUSED_STATE_SET, ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, ENABLED_SELECTED_STATE_SET, ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, ENABLED_STATE_SET, ENABLED_WINDOW_FOCUSED_STATE_SET, FOCUS_BACKWARD, FOCUS_DOWN, FOCUS_FORWARD, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_UP, FOCUSABLE, FOCUSABLE_AUTO, FOCUSABLES_ALL, FOCUSABLES_TOUCH_MODE, FOCUSED_SELECTED_STATE_SET, FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, FOCUSED_STATE_SET, FOCUSED_WINDOW_FOCUSED_STATE_SET, GONE, HAPTIC_FEEDBACK_ENABLED, INVISIBLE, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE, LAYOUT_DIRECTION_LTR, LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_UNDEFINED, LEFT, MEASURED_HEIGHT_STATE_SHIFT, MEASURED_SIZE_MASK, MEASURED_STATE_MASK, MEASURED_STATE_TOO_SMALL, mPaddingBottom, mPaddingLeft, mPaddingRight, mPaddingTop, mScrollX, mScrollY, NO_ID, NOT_FOCUSABLE, OVER_SCROLL_ALWAYS, OVER_SCROLL_IF_CONTENT_SCROLLS, OVER_SCROLL_NEVER, PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, PRESSED_ENABLED_FOCUSED_STATE_SET, PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, PRESSED_ENABLED_SELECTED_STATE_SET, PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, PRESSED_ENABLED_STATE_SET, PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET, PRESSED_FOCUSED_SELECTED_STATE_SET, PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, PRESSED_FOCUSED_STATE_SET, PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET, PRESSED_SELECTED_STATE_SET, PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET, PRESSED_STATE_SET, PRESSED_WINDOW_FOCUSED_STATE_SET, RIGHT, ROTATION, ROTATION_X, ROTATION_Y, SCALE_X, SCALE_Y, SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_NONE, SCROLL_AXIS_VERTICAL, SCROLL_INDICATOR_BOTTOM, SCROLL_INDICATOR_END, SCROLL_INDICATOR_LEFT, SCROLL_INDICATOR_RIGHT, SCROLL_INDICATOR_START, SCROLL_INDICATOR_TOP, SCROLL_X, SCROLL_Y, SCROLLBARS_INSIDE_INSET, SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_OUTSIDE_INSET, SCROLLBARS_OUTSIDE_OVERLAY, SELECTED_STATE_SET, SELECTED_WINDOW_FOCUSED_STATE_SET, SOUND_EFFECTS_ENABLED, TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_GRAVITY, TEXT_ALIGNMENT_INHERIT, TEXT_ALIGNMENT_TEXT_END, TEXT_ALIGNMENT_TEXT_START, TEXT_ALIGNMENT_VIEW_END, TEXT_ALIGNMENT_VIEW_START, TEXT_DIRECTION_ANY_RTL, TEXT_DIRECTION_FIRST_STRONG, TEXT_DIRECTION_FIRST_STRONG_LTR, TEXT_DIRECTION_FIRST_STRONG_RTL, TEXT_DIRECTION_INHERIT, TEXT_DIRECTION_LOCALE, TEXT_DIRECTION_LTR, TEXT_DIRECTION_RTL, TOP, TRANSLATION_X, TRANSLATION_Y, TRANSLATION_Z, TYPE_NON_TOUCH, TYPE_TOUCH, VIEW_MARKER, VISIBLE, WINDOW_FOCUSED_STATE_SET, X, Y, Z
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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
addTouchables
(ArrayList<View> views) We only want the current page that is being shown to be touchable.void
addView
(View child, int index, ViewGroup.LayoutParams params) Adds a child view with the specified layout parameters.boolean
arrowScroll
(int direction) Handle scrolling in response to a left or right arrow click.boolean
Start a fake drag of the pager.protected boolean
Tests scrollability within child views of v given a delta of dx.boolean
canScrollHorizontally
(int direction) Check if this ViewPager can be scrolled horizontally in a certain direction.protected boolean
Check whether given params fit to this view group.void
Remove all listeners that are notified of any changes in scroll state or position.void
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.boolean
dispatchKeyEvent
(KeyEvent event) Dispatch a key event to the next view on the focus path.void
Base method that directly draws this view and its background, foreground, overlay and all children to the given canvas.protected void
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.void
End a fake drag of the pager.boolean
executeKeyEvent
(KeyEvent event) 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.protected ViewGroup.LayoutParams
Returns a set of default layout parameters.protected ViewGroup.LayoutParams
Returns a safe set of layout parameters based on the supplied layout params.Retrieve the current adapter supplying pages.protected int
getChildDrawingOrder
(int childCount, int i) Converts drawing order position to container position.int
int
Returns the left edge effect color.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.int
Returns the right edge effect color.boolean
Returns true if a fake drag is in progress.protected void
This is called when the view is attached to a window.protected void
This is called when the view is detached from a window.protected void
Draw the content of this view, implement this to do your drawing.boolean
Implement this method to intercept all touch screen motion events.protected void
onLayout
(boolean changed, int l, int t, int r, int b) Called fromView.layout(int, int, int, int)
when this view should assign a size and position to each of its children.protected void
onMeasure
(int widthMeasureSpec, int heightMeasureSpec) Measure the view and its content to determine the measured width and the measured height.protected void
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.protected boolean
onRequestFocusInDescendants
(int direction, Rect previouslyFocusedRect) We only want the current page that is being shown to be focusable.protected void
onSizeChanged
(int w, int h, int oldw, int oldh) Called when width or height changedboolean
Implement this method to handle touch screen motion events.void
Remove a listener that was previously added viaaddOnAdapterChangeListener(OnAdapterChangeListener)
.void
Remove a listener that was previously added viaaddOnPageChangeListener(OnPageChangeListener)
.void
removeView
(View view) Note: do not invoke this method fromView.draw(Canvas)
,View.onDraw(Canvas)
,ViewGroup.dispatchDraw(Canvas)
or any related method.void
setAdapter
(PagerAdapter adapter) 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
setEdgeEffectColor
(int color) Sets the edge effect color for both left and right edge effects.void
setLeftEdgeEffectBlendMode
(BlendMode blendMode) Sets the left edge effect blend mode, the default isEdgeEffect.DEFAULT_BLEND_MODE
.void
setLeftEdgeEffectColor
(int color) Sets the left edge effect color.void
setOffscreenPageLimit
(int limit) 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
setPageMargin
(int marginPixels) Set the margin between pages.void
Set a drawable that will be used to fill the margin between pages.void
setPageTransformer
(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer) Sets aViewPager.PageTransformer
that will be called for each attached page whenever the scroll position is changed.void
setPageTransformer
(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer, int pageLayerType) Sets aViewPager.PageTransformer
that will be called for each attached page whenever the scroll position is changed.void
setRightEdgeEffectBlendMode
(BlendMode blendMode) Sets the right edge effect blend mode, the default isEdgeEffect.DEFAULT_BLEND_MODE
.void
setRightEdgeEffectColor
(int color) Sets the right edge effect color.protected boolean
verifyDrawable
(Drawable drawable) If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.Methods inherited from class icyllis.modernui.view.ViewGroup
addKeyboardNavigationClusters, addStatesFromChildren, addTransientView, addView, addView, addView, addView, addViewInLayout, addViewInLayout, attachViewToParent, bringChildToFront, childDrawableStateChanged, childHasTransientStateChanged, cleanupLayoutState, clearChildFocus, clearDisappearingChildren, clearFocus, detachAllViewsFromParent, detachViewFromParent, detachViewFromParent, detachViewsFromParent, dispatchDraw, dispatchDrawableHotspotChanged, dispatchFinishTemporaryDetach, dispatchGenericPointerEvent, dispatchHoverEvent, dispatchKeyShortcutEvent, dispatchSetActivated, dispatchSetPressed, dispatchSetSelected, dispatchStartTemporaryDetach, dispatchTouchEvent, dispatchVisibilityChanged, dispatchWindowFocusChanged, dispatchWindowVisibilityChanged, drawChild, endViewTransition, findFocus, findViewByPredicateTraversal, findViewTraversal, focusableViewAvailable, focusSearch, getChildAt, getChildCount, getChildDrawingOrder, getChildMeasureSpec, getChildVisibleRect, getChildVisibleRect, getClipChildren, getClipToPadding, getDescendantFocusability, getFocusedChild, getLayoutTransition, getNestedScrollAxes, getTouchscreenBlocksFocus, getTransientView, getTransientViewCount, getTransientViewIndex, hasBooleanFlag, hasFocus, hasTransientState, indexOfChild, internalSetPadding, isChildrenDrawingOrderEnabled, isLayoutSuppressed, isTransitionGroup, jumpDrawablesToCurrentState, layout, measureChild, measureChildren, measureChildWithMargins, offsetChildrenTopAndBottom, offsetDescendantRectToMyCoords, offsetRectIntoDescendantCoords, onChildVisibilityChanged, onCreateDrawableState, onInterceptHoverEvent, onNestedFling, onNestedPreFling, onNestedPreScroll, onNestedScroll, onNestedScrollAccepted, onResolvePointerIcon, onSetLayoutParams, onStartNestedScroll, onStopNestedScroll, onViewAdded, onViewRemoved, removeAllViews, removeAllViewsInLayout, removeDetachedView, removeTransientView, removeViewAt, removeViewInLayout, removeViews, removeViewsInLayout, requestChildFocus, requestChildRectangleOnScreen, requestDisallowInterceptTouchEvent, requestFocus, requestTransitionStart, resetResolvedDrawables, resolveDrawables, resolveLayoutDirection, resolveLayoutParams, resolvePadding, resolveRtlPropertiesIfNeeded, resolveTextAlignment, resolveTextDirection, restoreDefaultFocus, setAddStatesFromChildren, setBooleanFlag, setChildrenDrawingOrderEnabled, setClipChildren, setClipToPadding, setDescendantFocusability, setLayoutTransition, setTouchscreenBlocksFocus, setTransitionGroup, shouldDelayChildPressedState, showContextMenuForChild, startActionModeForChild, startViewTransition, suppressLayout, transformPointToViewLocal, updateViewLayout
Methods inherited from class icyllis.modernui.view.View
addFocusables, addOnAttachStateChangeListener, addOnLayoutChangeListener, awakenScrollBars, awakenScrollBars, callOnClick, cancelLongPress, cancelPendingInputEvents, canReceivePointerEvents, canResolveLayoutDirection, canResolveTextAlignment, canResolveTextDirection, canScrollVertically, combineMeasuredStates, computeHorizontalScrollExtent, computeHorizontalScrollOffset, computeHorizontalScrollRange, computeVerticalScrollExtent, computeVerticalScrollOffset, computeVerticalScrollRange, createContextMenu, dispatchGenericMotionEvent, dispatchNestedFling, dispatchNestedPreFling, dispatchNestedPreScroll, dispatchNestedScroll, dispatchPointerEvent, dp, drawableHotspotChanged, findViewById, findViewByPredicate, findViewByPredicateInsideOut, focusSearch, forceHasOverlappingRendering, forceLayout, generateViewId, getAlpha, getAnimationMatrix, getBackground, getBaseline, getBottom, getBoundsOnScreen, getBoundsOnScreen, getContext, getContextMenuInfo, getDefaultSize, getDrawableState, getDrawingRect, getElevation, getFadingEdge, getFadingEdgeLength, getFocusable, getFocusables, getFocusedRect, getForeground, getForegroundGravity, getGlobalVisibleRect, getGlobalVisibleRect, getHandler, getHasOverlappingRendering, getHeight, getHitRect, getHorizontalScrollbarHeight, getHorizontalScrollbarThumbDrawable, getHorizontalScrollbarTrackDrawable, getId, getInverseMatrix, getKeyDispatcherState, getLayoutDirection, getLayoutParams, getLeft, getLocalVisibleRect, getLocationInWindow, getLocationOnScreen, getMatrix, getMeasuredHeight, getMeasuredHeightAndState, getMeasuredState, getMeasuredWidth, getMeasuredWidthAndState, getMinimumHeight, getMinimumWidth, getNextClusterForwardId, getNextFocusDownId, getNextFocusForwardId, getNextFocusLeftId, getNextFocusRightId, getNextFocusUpId, getOnFocusChangeListener, getOnLongClickListener, getOverScrollMode, getPaddingBottom, getPaddingEnd, getPaddingLeft, getPaddingRight, getPaddingStart, getPaddingTop, getParent, getPivotX, getPivotY, getRawLayoutDirection, getRawTextAlignment, getRawTextDirection, getRevealOnFocusHint, getRight, getRootView, getRotation, getRotationX, getRotationY, getScaleX, getScaleY, getScrollBarDefaultDelayBeforeFade, getScrollBarFadeDuration, getScrollBarSize, getScrollBarStyle, getScrollIndicators, getScrollX, getScrollY, getStateListAnimator, getSuggestedMinimumHeight, getSuggestedMinimumWidth, getTag, getTextAlignment, getTextDirection, getTooltipText, getTooltipView, getTop, getTouchables, getTransitionAlpha, getTransitionName, getTranslationX, getTranslationY, getTranslationZ, getVerticalFadingEdgeLength, getVerticalScrollbarThumbDrawable, getVerticalScrollbarTrackDrawable, getVerticalScrollbarWidth, getViewRoot, getViewTreeObserver, getVisibility, getWidth, getWindowAttachCount, getWindowVisibility, getX, getY, getZ, handleScrollBarDragging, hasExplicitFocusable, hasFocusable, hasIdentityMatrix, hasNestedScrollingParent, hasOnClickListeners, hasOnLongClickListeners, hasOverlappingRendering, hasTranslationTransientState, hasWindowFocus, invalidate, invalidateDrawable, isActivated, isAttachedToWindow, isClickable, isContextClickable, isDuplicateParentStateEnabled, isEnabled, isFocusable, isFocusableInTouchMode, isFocused, isFocusedByDefault, isForegroundInsidePadding, isHapticFeedbackEnabled, isHorizontalFadingEdgeEnabled, isHorizontalScrollBarEnabled, isHovered, isInLayout, isInScrollingContainer, isInTouchMode, isKeyboardNavigationCluster, isLaidOut, isLayoutDirectionInherited, isLayoutDirectionResolved, isLayoutRequested, isLayoutRtl, isLongClickable, isNestedScrollingEnabled, isPaddingRelative, isPivotSet, isPressed, isRootNamespace, isScrollbarFadingEnabled, isSelected, isShowingLayoutBounds, isShown, isSoundEffectsEnabled, isTemporarilyDetached, isTextAlignmentInherited, isTextAlignmentResolved, isTextDirectionInherited, isTextDirectionResolved, isVerticalFadingEdgeEnabled, isVerticalScrollBarEnabled, isVerticalScrollBarHidden, keyboardNavigationClusterSearch, mapRectFromViewToScreenCoords, measure, mergeDrawableStates, offsetLeftAndRight, offsetTopAndBottom, onCancelPendingInputEvents, onCreateContextMenu, onDragEvent, onDrawForeground, onDrawScrollBars, onFinishTemporaryDetach, onFocusChanged, onGenericMotionEvent, onHoverChanged, onHoverEvent, onKeyDown, onKeyShortcut, onKeyUp, onOverScrolled, onResolveDrawables, onRtlPropertiesChanged, onScrollChanged, onStartTemporaryDetach, onVisibilityAggregated, onVisibilityChanged, onWindowFocusChanged, onWindowVisibilityChanged, overScrollBy, performButtonActionOnTouchDown, performClick, performContextClick, performContextClick, performHapticFeedback, performHapticFeedback, performLongClick, performLongClick, playSoundEffect, pointInView, post, postDelayed, postInvalidate, postInvalidateDelayed, postInvalidateOnAnimation, postOnAnimation, postOnAnimationDelayed, refreshDrawableState, removeCallbacks, removeOnAttachStateChangeListener, removeOnLayoutChangeListener, requestFocus, requestFocus, requestLayout, requestRectangleOnScreen, requestRectangleOnScreen, requireViewById, resetPivot, resolveSize, resolveSizeAndState, scheduleDrawable, scrollBy, scrollTo, setActivated, setAlpha, setAnimationMatrix, setBackground, setBottom, setClickable, setContextClickable, setDuplicateParentStateEnabled, setElevation, setEnabled, setFocusable, setFocusable, setFocusableInTouchMode, setFocusedByDefault, setFocusedInCluster, setForeground, setForegroundGravity, setFrame, setHapticFeedbackEnabled, setHasTransientState, setHasTranslationTransientState, setHorizontalFadingEdgeEnabled, setHorizontalScrollBarEnabled, setHorizontalScrollbarThumbDrawable, setHorizontalScrollbarTrackDrawable, setHovered, setId, setIsRootNamespace, setKeyboardNavigationCluster, setLayoutDirection, setLayoutParams, setLeft, setLongClickable, setMeasuredDimension, setMinimumHeight, setMinimumWidth, setNestedScrollingEnabled, setNextClusterForwardId, setNextFocusDownId, setNextFocusForwardId, setNextFocusLeftId, setNextFocusRightId, setNextFocusUpId, setOnClickListener, setOnContextClickListener, setOnCreateContextMenuListener, setOnDragListener, setOnFocusChangeListener, setOnGenericMotionListener, setOnHoverListener, setOnKeyListener, setOnLongClickListener, setOnScrollChangeListener, setOnTouchListener, setOverScrollMode, setPadding, setPaddingRelative, setPivotX, setPivotY, setPressed, setRevealOnFocusHint, setRight, setRotation, setRotationX, setRotationY, setScaleX, setScaleY, setScrollBarDefaultDelayBeforeFade, setScrollBarFadeDuration, setScrollbarFadingEnabled, setScrollBarSize, setScrollBarStyle, setScrollIndicators, setScrollIndicators, setScrollX, setScrollY, setSelected, setSoundEffectsEnabled, setStateListAnimator, setTag, setTextAlignment, setTextDirection, setTooltipText, setTop, setTransitionAlpha, setTransitionName, setTransitionVisibility, setTranslationX, setTranslationY, setTranslationZ, setVerticalFadingEdgeEnabled, setVerticalScrollBarEnabled, setVerticalScrollbarThumbDrawable, setVerticalScrollbarTrackDrawable, setVisibility, setWillNotDraw, setX, setY, setZ, showContextMenu, showContextMenu, sp, startActionMode, startActionMode, startDragAndDrop, startNestedScroll, stopNestedScroll, toGlobalMotionEvent, toLocalMotionEvent, toString, transformFromViewToWindowSpace, transformMatrixToGlobal, transformMatrixToLocal, unscheduleDrawable, unscheduleDrawable, willNotDraw
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface icyllis.modernui.view.ViewParent
canResolveLayoutDirection, canResolveTextAlignment, canResolveTextDirection, createContextMenu, getLayoutDirection, getParent, getTextAlignment, getTextDirection, isLayoutDirectionResolved, isLayoutRequested, isTextAlignmentResolved, isTextDirectionResolved, keyboardNavigationClusterSearch, requestLayout
-
Field Details
-
SCROLL_STATE_IDLE
public static final int SCROLL_STATE_IDLEIndicates that the pager is in an idle, settled state. The current page is fully in view and no animation is in progress.- See Also:
-
SCROLL_STATE_DRAGGING
public static final int SCROLL_STATE_DRAGGINGIndicates that the pager is currently being dragged by the user.- See Also:
-
SCROLL_STATE_SETTLING
public static final int SCROLL_STATE_SETTLINGIndicates that the pager is in the process of settling to a final position.- See Also:
-
-
Constructor Details
-
ViewPager
-
-
Method Details
-
onDetachedFromWindow
protected void onDetachedFromWindow()Description copied from class:View
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.- Overrides:
onDetachedFromWindow
in classView
- See Also:
-
setAdapter
Set a PagerAdapter that will supply views for this pager as needed.- Parameters:
adapter
- Adapter to use
-
getAdapter
Retrieve the current adapter supplying pages.- Returns:
- The currently registered PagerAdapter
-
addOnAdapterChangeListener
Add a listener that will be invoked whenever the adapter for this ViewPager changes.- Parameters:
listener
- listener to add
-
removeOnAdapterChangeListener
Remove a listener that was previously added viaaddOnAdapterChangeListener(OnAdapterChangeListener)
.- Parameters:
listener
- listener to remove
-
setCurrentItem
public void setCurrentItem(int item) 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
- Item index to select
-
setCurrentItem
public void setCurrentItem(int item, boolean smoothScroll) Set the currently selected page.- Parameters:
item
- Item index to selectsmoothScroll
- True to smoothly scroll to the new item, false to transition immediately
-
getCurrentItem
public int getCurrentItem() -
addOnPageChangeListener
Add a listener that will be invoked whenever the page changes or is incrementally scrolled. SeeViewPager.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
- listener to add
-
removeOnPageChangeListener
Remove a listener that was previously added viaaddOnPageChangeListener(OnPageChangeListener)
.- Parameters:
listener
- listener to remove
-
clearOnPageChangeListeners
public void clearOnPageChangeListeners()Remove all listeners that are notified of any changes in scroll state or position. -
setPageTransformer
public void setPageTransformer(boolean reverseDrawingOrder, @Nullable ViewPager.PageTransformer transformer) Sets aViewPager.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
- true if the supplied PageTransformer requires page views to be drawn from last to first instead of first to last.transformer
- PageTransformer that will modify each page's animation properties
-
setPageTransformer
public void setPageTransformer(boolean reverseDrawingOrder, @Nullable ViewPager.PageTransformer transformer, int pageLayerType) Sets aViewPager.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
- true if the supplied PageTransformer requires page views to be drawn from last to first instead of first to last.transformer
- PageTransformer that will modify each page's animation propertiespageLayerType
- View layer type that should be used for ViewPager pages. It should be eitherinvalid reference
View#LAYER_TYPE_HARDWARE
invalid reference
View#LAYER_TYPE_SOFTWARE
invalid reference
View#LAYER_TYPE_NONE
-
getOffscreenPageLimit
public int getOffscreenPageLimit()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:
- How many pages will be kept offscreen on either side
- See Also:
-
setOffscreenPageLimit
public void setOffscreenPageLimit(int limit) 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
- How many pages will be kept offscreen in an idle state.
-
setPageMargin
public void setPageMargin(int marginPixels) Set the margin between pages.- Parameters:
marginPixels
- Distance between adjacent pages in pixels- See Also:
-
getPageMargin
public int getPageMargin()Return the margin between pages.- Returns:
- The size of the margin in pixels
-
setPageMarginDrawable
Set a drawable that will be used to fill the margin between pages.- Parameters:
d
- Drawable to display between pages
-
setEdgeEffectColor
public void setEdgeEffectColor(int color) Sets the edge effect color for both left and right edge effects.- Parameters:
color
- The color for the edge effects.- See Also:
-
setLeftEdgeEffectColor
public void setLeftEdgeEffectColor(int color) Sets the left edge effect color.- Parameters:
color
- The color for the left edge effect.- See Also:
-
setRightEdgeEffectColor
public void setRightEdgeEffectColor(int color) Sets the right edge effect color.- Parameters:
color
- The color for the right edge effect.- See Also:
-
getLeftEdgeEffectColor
public int getLeftEdgeEffectColor()Returns the left edge effect color.- Returns:
- The left edge effect color.
- See Also:
-
getRightEdgeEffectColor
public int getRightEdgeEffectColor()Returns the right edge effect color.- Returns:
- The right edge effect color.
- See Also:
-
setLeftEdgeEffectBlendMode
Sets the left edge effect blend mode, the default isEdgeEffect.DEFAULT_BLEND_MODE
.- Parameters:
blendMode
- The blend mode for the left edge effect.
-
setRightEdgeEffectBlendMode
Sets the right edge effect blend mode, the default isEdgeEffect.DEFAULT_BLEND_MODE
.- Parameters:
blendMode
- The blend mode for the right edge effect.
-
getLeftEdgeEffectBlendMode
-
getRightEdgeEffectBlendMode
-
verifyDrawable
Description copied from class:View
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.Be sure to call through to the super class when overriding this function.
- Overrides:
verifyDrawable
in classView
- Parameters:
drawable
- The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.- Returns:
- boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.
- See Also:
-
drawableStateChanged
protected void drawableStateChanged()Description copied from class:View
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.If the View has a StateListAnimator, it will also be called to run necessary state change animations.
Be sure to call through to the superclass when overriding this function.
- Overrides:
drawableStateChanged
in classViewGroup
- See Also:
-
getChildDrawingOrder
protected int getChildDrawingOrder(int childCount, int i) Description copied from class:ViewGroup
Converts drawing order position to container position. Override this if you want to change the drawing order of children. By default, it returns drawingPosition.NOTE: In order for this method to be called, you must enable child ordering first by calling
ViewGroup.setChildrenDrawingOrderEnabled(boolean)
.- Overrides:
getChildDrawingOrder
in classViewGroup
- Parameters:
i
- the drawing order position.- Returns:
- the container position of a child for this drawing order position.
- See Also:
-
addView
Description copied from class:ViewGroup
Adds a child view with the specified layout parameters.Note: do not invoke this method from
View.draw(Canvas)
,View.onDraw(Canvas)
,ViewGroup.dispatchDraw(Canvas)
or any related method. -
removeView
Description copied from class:ViewGroup
Note: do not invoke this method from
View.draw(Canvas)
,View.onDraw(Canvas)
,ViewGroup.dispatchDraw(Canvas)
or any related method.- Specified by:
removeView
in interfaceViewManager
- Overrides:
removeView
in classViewGroup
-
onAttachedToWindow
protected void onAttachedToWindow()Description copied from class:View
This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called beforeView.onDraw(Canvas)
, however it may be called any time before the first onDraw -- including before or afterView.onMeasure(int, int)
.- Overrides:
onAttachedToWindow
in classView
- See Also:
-
onMeasure
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) Description copied from class:View
Measure the view and its content to determine the measured width and the measured height. This method is invoked byView.measure(int, int)
and should be overridden by subclasses to provide accurate and efficient measurement of their contents.CONTRACT: When overriding this method, you must call
View.setMeasuredDimension(int, int)
to store the measured width and height of this view. Failure to do so will trigger anIllegalStateException
, thrown byView.measure(int, int)
. Calling super.onMeasure() is a valid use.The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override the base one to provide better measurements of their content.
- Overrides:
onMeasure
in classView
- Parameters:
widthMeasureSpec
- width measure specification imposed by the parentMeasureSpec
heightMeasureSpec
- height measure specification imposed by the parentMeasureSpec
-
onSizeChanged
protected void onSizeChanged(int w, int h, int oldw, int oldh) Description copied from class:View
Called when width or height changed- Overrides:
onSizeChanged
in classView
- Parameters:
w
- new widthh
- new heightoldw
- previous widtholdh
- previous height
-
onLayout
protected void onLayout(boolean changed, int l, int t, int r, int b) Description copied from class:View
Called fromView.layout(int, int, int, int)
when this view should assign a size and position to each of its children.Derived classes with children should override this method and call layout on each of their children.
-
computeScroll
public void computeScroll()Description copied from class:View
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. This will typically be done if the child is animating a scroll using a Scroller.- Overrides:
computeScroll
in classView
-
onPageScrolled
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.- Parameters:
position
- Position index of the first page currently being displayed. Page position+1 will be visible if positionOffset is nonzero.offset
- Value from [0, 1) indicating the offset from the page at position.offsetPixels
- Value in pixels indicating the offset from position.
-
onInterceptTouchEvent
Description copied from class:ViewGroup
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.Using this function takes some care, as it has a fairly complicated interaction with
View.onTouchEvent(MotionEvent)
, and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:- You will receive the down event here.
- The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
- For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
- If you return true from here, you will not receive any
following events: the target view will receive the same event but
with the action
MotionEvent.ACTION_CANCEL
, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
- Overrides:
onInterceptTouchEvent
in classViewGroup
- Parameters:
ev
- The motion event being dispatched down the hierarchy.- Returns:
- Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.
-
onTouchEvent
Description copied from class:View
Implement this method to handle touch screen motion events.If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling
View.performClick()
. This will ensure consistent system behavior.- Overrides:
onTouchEvent
in classView
- Parameters:
ev
- the touch event- Returns:
true
if the event was handled by the view,false
otherwise
-
draw
Description copied from class:View
Base method that directly draws this view and its background, foreground, overlay and all children to the given canvas. When implementing a view, overrideView.onDraw(Canvas)
instead of this.This is not the entry point for the view system to draw.
-
onDraw
Description copied from class:View
Draw the content of this view, implement this to do your drawing.Note that (0, 0) will be the top left of the bounds, and (width, height) will be the bottom right of the bounds.
-
beginFakeDrag
public boolean beginFakeDrag()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(float)
to simulate the actual drag motion. CallendFakeDrag()
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:
- true if the fake drag began successfully, false if it could not be started.
- See Also:
-
endFakeDrag
public void endFakeDrag()End a fake drag of the pager.- See Also:
-
fakeDragBy
public void fakeDragBy(float xOffset) Fake drag by an offset in pixels. You must have calledbeginFakeDrag()
first.- Parameters:
xOffset
- Offset in pixels to drag by.- See Also:
-
isFakeDragging
public boolean isFakeDragging()Returns true if a fake drag is in progress.- Returns:
- true if currently in a fake drag, false otherwise.
- See Also:
-
canScrollHorizontally
public boolean canScrollHorizontally(int direction) Check if this ViewPager can be scrolled horizontally in a certain direction.- Overrides:
canScrollHorizontally
in classView
- Parameters:
direction
- Negative to check scrolling left, positive to check scrolling right.- Returns:
- Whether this ViewPager can be scrolled in the specified direction. It will always return false if the specified direction is 0.
-
canScroll
Tests scrollability within child views of v given a delta of dx.- Parameters:
v
- View to test for horizontal scrollabilitycheckV
- Whether the view v passed should itself be checked for scrollability (true), or just its children (false).dx
- Delta scrolled in pixelsx
- X coordinate of the active touch pointy
- Y coordinate of the active touch point- Returns:
- true if child views of v can be scrolled by delta of dx.
-
dispatchKeyEvent
Description copied from class:View
Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.- Overrides:
dispatchKeyEvent
in classViewGroup
- Parameters:
event
- The key event to be dispatched.- Returns:
- True if the event was handled, false otherwise.
-
executeKeyEvent
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
- The key event to execute.- Returns:
- Return true if the event was handled, else false.
-
arrowScroll
public boolean arrowScroll(int direction) Handle scrolling in response to a left or right arrow click.- Parameters:
direction
- The direction corresponding to the arrow key that was pressed. It should be eitherView.FOCUS_LEFT
orView.FOCUS_RIGHT
.- Returns:
- Whether the scrolling was handled successfully.
-
addFocusables
We only want the current page that is being shown to be focusable.- Overrides:
addFocusables
in classViewGroup
- Parameters:
views
- Focusable views found so far or null if all we are interested is the number of focusables.direction
- The direction of the focus.
-
addTouchables
We only want the current page that is being shown to be touchable.- Overrides:
addTouchables
in classViewGroup
- Parameters:
views
- Touchable views found so far
-
onRequestFocusInDescendants
We only want the current page that is being shown to be focusable.- Overrides:
onRequestFocusInDescendants
in classViewGroup
- Parameters:
direction
- One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHTpreviouslyFocusedRect
- The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.- Returns:
- Whether focus was taken.
-
generateDefaultLayoutParams
Description copied from class:ViewGroup
Returns a set of default layout parameters. These parameters are requested when the View passed toViewGroup.addView(View)
has no layout parameters already set. If null is returned, an exception is thrown from addView.- Overrides:
generateDefaultLayoutParams
in classViewGroup
- Returns:
- a set of default layout parameters
-
generateLayoutParams
Description copied from class:ViewGroup
Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test ofViewGroup.checkLayoutParams(LayoutParams)
, this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.- Overrides:
generateLayoutParams
in classViewGroup
- Parameters:
p
- The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.- Returns:
- an instance of
ViewGroup.LayoutParams
or one of its descendants
-
checkLayoutParams
Description copied from class:ViewGroup
Check whether given params fit to this view group.See also
ViewGroup.generateLayoutParams(LayoutParams)
See alsoViewGroup.generateDefaultLayoutParams()
- Overrides:
checkLayoutParams
in classViewGroup
- Parameters:
p
- layout params to check- Returns:
- if params matched to this view group
-