PagerSnapHelper
public class PagerSnapHelper extends SnapHelper
| java.lang.Object | |||
| ↳ | androidx.recyclerview.widget.RecyclerView.OnFlingListener | ||
| ↳ | androidx.recyclerview.widget.SnapHelper | ||
| ↳ | androidx.recyclerview.widget.PagerSnapHelper |
Implementation of the SnapHelper supporting pager style snapping in either vertical or horizontal orientation.
PagerSnapHelper can help achieve a similar behavior to androidx.viewpager.widget.ViewPager. Set both RecyclerView and the items of the RecyclerView.Adapter to have MATCH_PARENT height and width and then attach PagerSnapHelper to the RecyclerView using attachToRecyclerView.
Summary
Public constructors |
|---|
Public methods |
|
|---|---|
@Nullable int[] |
calculateDistanceToFinalSnap(Override this method to snap to a particular point within the target view or the container view on any axis. |
@Nullable View |
findSnapView(RecyclerView.LayoutManager layoutManager)Override this method to provide a particular target view for snapping. |
int |
findTargetSnapPosition(Override to provide a particular adapter target position for snapping. |
Protected methods |
|
|---|---|
@Nullable RecyclerView.SmoothScroller |
createScroller(@NonNull RecyclerView.LayoutManager layoutManager)Creates a scroller to be used in the snapping implementation. |
Inherited methods |
||||||||
|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public methods
calculateDistanceToFinalSnap
public @Nullable int[] calculateDistanceToFinalSnap(
@NonNull RecyclerView.LayoutManager layoutManager,
@NonNull View targetView
)
Override this method to snap to a particular point within the target view or the container view on any axis.
This method is called when the SnapHelper has intercepted a fling and it needs to know the exact distance required to scroll by in order to snap to the target view.
| Parameters | |
|---|---|
@NonNull RecyclerView.LayoutManager layoutManager |
the |
@NonNull View targetView |
the target view that is chosen as the view to snap |
| Returns | |
|---|---|
@Nullable int[] |
the output coordinates the put the result into. out[0] is the distance on horizontal axis and out[1] is the distance on vertical axis. |
findSnapView
public @Nullable View findSnapView(RecyclerView.LayoutManager layoutManager)
Override this method to provide a particular target view for snapping.
This method is called when the SnapHelper is ready to start snapping and requires a target view to snap to. It will be explicitly called when the scroll state becomes idle after a scroll. It will also be called when the SnapHelper is preparing to snap after a fling and requires a reference view from the current set of child views.
If this method returns null, SnapHelper will not snap to any view.
| Parameters | |
|---|---|
RecyclerView.LayoutManager layoutManager |
the |
findTargetSnapPosition
public int findTargetSnapPosition(
RecyclerView.LayoutManager layoutManager,
int velocityX,
int velocityY
)
Override to provide a particular adapter target position for snapping.
| Parameters | |
|---|---|
RecyclerView.LayoutManager layoutManager |
the |
int velocityX |
fling velocity on the horizontal axis |
int velocityY |
fling velocity on the vertical axis |
| Returns | |
|---|---|
int |
the target adapter position to you want to snap or |
Protected methods
createScroller
protected @Nullable RecyclerView.SmoothScroller createScroller(@NonNull RecyclerView.LayoutManager layoutManager)
Creates a scroller to be used in the snapping implementation.
| Parameters | |
|---|---|
@NonNull RecyclerView.LayoutManager layoutManager |
The |
| Returns | |
|---|---|
@Nullable RecyclerView.SmoothScroller |
a |