ViewPager2
public final class ViewPager2 extends ViewGroup
| java.lang.Object | |||
| ↳ | android.view.View | ||
| ↳ | android.view.ViewGroup | ||
| ↳ | androidx.viewpager2.widget.ViewPager2 |
ViewPager2 replaces androidx.viewpager.widget.ViewPager, addressing most of its predecessor’s pain-points, including right-to-left layout support, vertical orientation, modifiable Fragment collections, etc.
A note on WindowInsets: due to a bug in API 29 and before, WindowInsets were incorrectly dispatched to a View's children and siblings. ViewPager2 offers a fix to make sure that all pages receive the correct insets, but it comes at the cost of not dispatching insets to any siblings of ViewPager2 or siblings of its ancestors that haven't received the insets yet. If you want to handle insets on each page separately (instead of on ViewPager2 or one of its parents), you should install a WindowInsetsApplier by calling WindowInsetsApplier.install(viewPager2). If you don't handle insets on any of the pages, we recommend you don't use the WindowInsetsApplier.
| See also | |
|---|---|
ViewPager |
Summary
Nested types |
|---|
public abstract class ViewPager2.OnPageChangeCallbackCallback interface for responding to changing state of the selected page. |
public interface ViewPager2.PageTransformerA PageTransformer is invoked whenever a visible/attached page is scrolled. |
Constants |
|
|---|---|
static final int |
Value to indicate that the default caching mechanism of RecyclerView should be used instead of explicitly prefetch and retain pages to either side of the current page. |
static final int |
|
static final int |
|
static final int |
Indicates that the ViewPager2 is currently being dragged by the user, or programmatically via fake drag functionality. |
static final int |
Indicates that the ViewPager2 is in an idle, settled state. |
static final int |
Indicates that the ViewPager2 is in the process of settling to a final position. |
Public constructors |
|---|
ViewPager2(@NonNull Context context) |
ViewPager2(@NonNull Context context, @Nullable AttributeSet attrs) |
ViewPager2( |
@RequiresApi(value = 21) |
Public methods |
|
|---|---|
void |
Add an |
void |
addItemDecoration(@NonNull RecyclerView.ItemDecoration decor, int index)Add an |
boolean |
Start a fake drag of the pager. |
boolean |
canScrollHorizontally(int direction) |
boolean |
canScrollVertically(int direction) |
boolean |
End a fake drag of the pager. |
boolean |
fakeDragBy(@Px float offsetPxFloat)Fake drag by an offset in pixels. |
CharSequence |
@RequiresApi(value = 23) |
@Nullable RecyclerView.Adapter |
|
int |
Returns the currently selected page. |
@NonNull RecyclerView.ItemDecoration |
getItemDecorationAt(int index)Returns an |
int |
Returns the number of |
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 |
|
int |
Returns the current scroll state of the ViewPager2. |
void |
Invalidates all ItemDecorations. |
boolean |
Returns |
boolean |
Returns if user initiated scrolling between pages is enabled. |
void |
|
void |
onViewAdded(View child) |
boolean |
performAccessibilityAction(int action, @Nullable Bundle arguments) |
void |
Add a callback that will be invoked whenever the page changes or is incrementally scrolled. |
void |
Remove an |
void |
removeItemDecorationAt(int index)Removes the |
void |
Trigger a call to the registered |
void |
setAdapter(@Nullable RecyclerView.Adapter adapter)Set a new adapter to provide page views on demand. |
void |
setCurrentItem(int item)Set the currently selected page. |
void |
setCurrentItem(int item, boolean smoothScroll)Set the currently selected page. |
void |
setLayoutDirection(int layoutDirection) |
void |
setOffscreenPageLimit(int limit)Set the number of pages that should be retained to either side of the currently visible page(s). |
void |
setOrientation(int orientation)Sets the orientation of the ViewPager2. |
void |
setPageTransformer(@Nullable ViewPager2.PageTransformer transformer)Sets a |
void |
setUserInputEnabled(boolean enabled)Enable or disable user initiated scrolling. |
void |
Remove a callback that was previously added via |
Protected methods |
|
|---|---|
void |
dispatchRestoreInstanceState(SparseArray<Parcelable> container) |
void |
onLayout(boolean changed, int l, int t, int r, int b) |
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
void |
onRestoreInstanceState(Parcelable state) |
@Nullable Parcelable |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
OFFSCREEN_PAGE_LIMIT_DEFAULT
public static final int OFFSCREEN_PAGE_LIMIT_DEFAULT = -1
Value to indicate that the default caching mechanism of RecyclerView should be used instead of explicitly prefetch and retain pages to either side of the current page.
| See also | |
|---|---|
setOffscreenPageLimit |
SCROLL_STATE_DRAGGING
public static final int SCROLL_STATE_DRAGGING = 1
Indicates that the ViewPager2 is currently being dragged by the user, or programmatically via fake drag functionality.
SCROLL_STATE_IDLE
public static final int SCROLL_STATE_IDLE = 0
Indicates that the ViewPager2 is in an idle, settled state. The current page is fully in view and no animation is in progress.
SCROLL_STATE_SETTLING
public static final int SCROLL_STATE_SETTLING = 2
Indicates that the ViewPager2 is in the process of settling to a final position.
Public constructors
ViewPager2
public ViewPager2(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr
)
ViewPager2
@RequiresApi(value = 21)
public ViewPager2(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes
)
Public methods
addItemDecoration
public void addItemDecoration(@NonNull RecyclerView.ItemDecoration decor)
Add an ItemDecoration to this ViewPager2. Item decorations can affect both measurement and drawing of individual item views.
Item decorations are ordered. Decorations placed earlier in the list will be run/queried/drawn first for their effects on item views. Padding added to views will be nested; a padding added by an earlier decoration will mean further item decorations in the list will be asked to draw/pad within the previous decoration's given area.
| Parameters | |
|---|---|
@NonNull RecyclerView.ItemDecoration decor |
Decoration to add |
addItemDecoration
public void addItemDecoration(@NonNull RecyclerView.ItemDecoration decor, int index)
Add an ItemDecoration to this ViewPager2. Item decorations can affect both measurement and drawing of individual item views.
Item decorations are ordered. Decorations placed earlier in the list will be run/queried/drawn first for their effects on item views. Padding added to views will be nested; a padding added by an earlier decoration will mean further item decorations in the list will be asked to draw/pad within the previous decoration's given area.
| Parameters | |
|---|---|
@NonNull RecyclerView.ItemDecoration decor |
Decoration to add |
int index |
Position in the decoration chain to insert this decoration at. If this value is negative the decoration will be added at the end. |
| Throws | |
|---|---|
java.lang.IndexOutOfBoundsException |
on indexes larger than |
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 ViewPager2 with the touch scrolling of another view, while still letting the ViewPager2 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.
A fake drag can be interrupted by a real drag. From that point on, all calls to
fakeDragBy and endFakeDrag will be ignored until the next fake drag is started by calling beginFakeDrag. If you need the ViewPager2 to ignore touch events and other user input during a fake drag, use setUserInputEnabled. If a real or fake drag is already in progress, this method will return false.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
fakeDragBy |
|
endFakeDrag |
|
isFakeDragging |
endFakeDrag
public boolean endFakeDrag()
End a fake drag of the pager.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
beginFakeDrag |
|
fakeDragBy |
|
isFakeDragging |
fakeDragBy
public boolean fakeDragBy(@Px float offsetPxFloat)
Fake drag by an offset in pixels. You must have called beginFakeDrag first. Drag happens in the direction of the orientation. Positive offsets will drag to the previous page, negative values to the next page, with one exception: if layout direction is set to RTL and the ViewPager2's orientation is horizontal, then the behavior will be inverted. This matches the deltas of touch events that would cause the same real drag.
If the pager is not in the fake dragging state anymore, it ignores this call and returns false.
| Parameters | |
|---|---|
@Px float offsetPxFloat |
Offset in pixels to drag by |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
beginFakeDrag |
|
endFakeDrag |
|
isFakeDragging |
getCurrentItem
public int getCurrentItem()
Returns the currently selected page. If no page can sensibly be selected because there is no adapter or the adapter is empty, returns 0.
| Returns | |
|---|---|
int |
Currently selected page |
getItemDecorationAt
public @NonNull RecyclerView.ItemDecoration getItemDecorationAt(int index)
Returns an ItemDecoration previously added to this ViewPager2.
| Parameters | |
|---|---|
int index |
The index position of the desired ItemDecoration. |
| Returns | |
|---|---|
@NonNull RecyclerView.ItemDecoration |
the ItemDecoration at index position |
| Throws | |
|---|---|
java.lang.IndexOutOfBoundsException |
on invalid index |
getItemDecorationCount
public int getItemDecorationCount()
Returns the number of ItemDecoration currently added to this ViewPager2.
| Returns | |
|---|---|
int |
number of ItemDecorations currently added added to this ViewPager2. |
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 OFFSCREEN_PAGE_LIMIT_DEFAULT.
| Returns | |
|---|---|
int |
How many pages will be kept offscreen on either side |
| See also | |
|---|---|
setOffscreenPageLimit |
getScrollState
public int getScrollState()
Returns the current scroll state of the ViewPager2. Returned value is one of can be one of SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING or SCROLL_STATE_SETTLING.
| Returns | |
|---|---|
int |
The scroll state that was last dispatched to |
invalidateItemDecorations
public void invalidateItemDecorations()
Invalidates all ItemDecorations. If ViewPager2 has item decorations, calling this method will trigger a requestLayout call.
isFakeDragging
public boolean isFakeDragging()
Returns true if a fake drag is in progress.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
beginFakeDrag |
|
fakeDragBy |
|
endFakeDrag |
isUserInputEnabled
public boolean isUserInputEnabled()
Returns if user initiated scrolling between pages is enabled. Enabled by default.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
setUserInputEnabled |
onInitializeAccessibilityNodeInfo
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
performAccessibilityAction
public boolean performAccessibilityAction(int action, @Nullable Bundle arguments)
registerOnPageChangeCallback
public void registerOnPageChangeCallback(
@NonNull ViewPager2.OnPageChangeCallback callback
)
Add a callback that will be invoked whenever the page changes or is incrementally scrolled. See OnPageChangeCallback.
Components that add a callback should take care to remove it when finished.
| Parameters | |
|---|---|
@NonNull ViewPager2.OnPageChangeCallback callback |
callback to add |
removeItemDecoration
public void removeItemDecoration(@NonNull RecyclerView.ItemDecoration decor)
Remove an ItemDecoration from this ViewPager2.
The given decoration will no longer impact the measurement and drawing of item views.
| Parameters | |
|---|---|
@NonNull RecyclerView.ItemDecoration decor |
Decoration to remove |
| See also | |
|---|---|
addItemDecoration |
removeItemDecorationAt
public void removeItemDecorationAt(int index)
Removes the ItemDecoration associated with the supplied index position.
| Parameters | |
|---|---|
int index |
The index position of the ItemDecoration to be removed. |
| Throws | |
|---|---|
java.lang.IndexOutOfBoundsException |
on invalid index |
requestTransform
public void requestTransform()
Trigger a call to the registered PageTransformer's transformPage method. Call this when something has changed which has invalidated the transformations defined by the PageTransformer that did not trigger a page scroll.
setAdapter
public void setAdapter(@Nullable RecyclerView.Adapter adapter)
Set a new adapter to provide page views on demand.
If you're planning to use Fragments as pages, implement FragmentStateAdapter. If your pages are Views, implement RecyclerView.Adapter as usual.
If your pages contain LayoutTransitions, then those LayoutTransitions must have animateParentHierarchy set to false. Note that if you have a ViewGroup with animateLayoutChanges="true" in your layout xml file, a LayoutTransition is added automatically to that ViewGroup. You will need to manually call getLayoutTransition().setAnimateParentHierarchy(false) on that ViewGroup after you inflated the xml layout, like this:
View view = layoutInflater.inflate(R.layout.page, parent, false); ViewGroup viewGroup = view.findViewById(R.id.animated_viewgroup); viewGroup.getLayoutTransition().setAnimateParentHierarchy(false);
| Parameters | |
|---|---|
@Nullable RecyclerView.Adapter adapter |
The adapter to use, or |
| See also | |
|---|---|
FragmentStateAdapter |
|
setAdapter |
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. Silently ignored if the adapter is not set or empty. Clamps item to the bounds of the adapter.
| Parameters | |
|---|---|
int item |
Item index to select |
setCurrentItem
public void setCurrentItem(int item, boolean smoothScroll)
Set the currently selected page. If smoothScroll = true, will perform a smooth animation from the current item to the new item. Silently ignored if the adapter is not set or empty. Clamps item to the bounds of the adapter.
| Parameters | |
|---|---|
int item |
Item index to select |
boolean smoothScroll |
True to smoothly scroll to the new item, false to transition immediately |
setOffscreenPageLimit
public void setOffscreenPageLimit(int limit)
Set the number of pages that should be retained to either side of the currently visible page(s). Pages beyond this limit will be recreated from the adapter when needed. Set this to OFFSCREEN_PAGE_LIMIT_DEFAULT to use RecyclerView's caching strategy. The given value must either be larger than 0, or #OFFSCREEN_PAGE_LIMIT_DEFAULT.
Pages within limit pages away from the current page are created and added to the view hierarchy, even though they are not visible on the screen. Pages outside this limit will be removed from the view hierarchy, but the ViewHolders will be recycled as usual by RecyclerView.
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. By default it is set to OFFSCREEN_PAGE_LIMIT_DEFAULT.
| Parameters | |
|---|---|
int limit |
How many pages will be kept offscreen on either side. Valid values are all values |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If the given limit is invalid |
| See also | |
|---|---|
getOffscreenPageLimit |
setOrientation
public void setOrientation(int orientation)
Sets the orientation of the ViewPager2.
| Parameters | |
|---|---|
int orientation |
setPageTransformer
public void setPageTransformer(@Nullable ViewPager2.PageTransformer transformer)
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: setting a PageTransformer disables data-set change animations to prevent conflicts between the two animation systems. Setting a null transformer will restore data-set change animations.
| Parameters | |
|---|---|
@Nullable ViewPager2.PageTransformer transformer |
PageTransformer that will modify each page's animation properties |
setUserInputEnabled
public void setUserInputEnabled(boolean enabled)
Enable or disable user initiated scrolling. This includes touch input (scroll and fling gestures) and accessibility input. Disabling keyboard input is not yet supported. When user initiated scrolling is disabled, programmatic scrolls through setCurrentItem still work. By default, user initiated scrolling is enabled.
| Parameters | |
|---|---|
boolean enabled |
|
| See also | |
|---|---|
isUserInputEnabled |
unregisterOnPageChangeCallback
public void unregisterOnPageChangeCallback(
@NonNull ViewPager2.OnPageChangeCallback callback
)
Remove a callback that was previously added via registerOnPageChangeCallback.
| Parameters | |
|---|---|
@NonNull ViewPager2.OnPageChangeCallback callback |
callback to remove |
Protected methods
dispatchRestoreInstanceState
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container)