SnapHelper
public abstract class SnapHelper extends RecyclerView.OnFlingListener
| java.lang.Object | ||
| ↳ | androidx.recyclerview.widget.RecyclerView.OnFlingListener | |
| ↳ | androidx.recyclerview.widget.SnapHelper |
LinearSnapHelper |
Implementation of the |
PagerSnapHelper |
Implementation of the |
Class intended to support snapping for a RecyclerView.
SnapHelper tries to handle fling as well but for this to work properly, the RecyclerView.LayoutManager must implement the RecyclerView.SmoothScroller.ScrollVectorProvider interface or you should override onFling and handle fling manually.
Summary
Public constructors |
|---|
Public methods |
|
|---|---|
void |
attachToRecyclerView(@Nullable RecyclerView recyclerView)Attaches the |
abstract @Nullable int[] |
calculateDistanceToFinalSnap(Override this method to snap to a particular point within the target view or the container view on any axis. |
int[] |
calculateScrollDistance(int velocityX, int velocityY)Calculated the estimated scroll distance in each direction given velocities on both axes. |
abstract @Nullable View |
findSnapView(RecyclerView.LayoutManager layoutManager)Override this method to provide a particular target view for snapping. |
abstract int |
findTargetSnapPosition(Override to provide a particular adapter target position for snapping. |
boolean |
onFling(int velocityX, int velocityY)Override this to handle a fling given the velocities in both x and y directions. |
Protected methods |
|
|---|---|
@Nullable RecyclerView.SmoothScroller |
createScroller(@NonNull RecyclerView.LayoutManager layoutManager)Creates a scroller to be used in the snapping implementation. |
@Nullable LinearSmoothScroller |
This method is deprecated. use |
Public constructors
Public methods
attachToRecyclerView
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener. You can call this method with null to detach it from the current RecyclerView.
| Parameters | |
|---|---|
@Nullable RecyclerView recyclerView |
The RecyclerView instance to which you want to add this helper or |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if there is already a |
java.lang.IllegalStateException |
calculateDistanceToFinalSnap
public abstract @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. |
calculateScrollDistance
public int[] calculateScrollDistance(int velocityX, int velocityY)
Calculated the estimated scroll distance in each direction given velocities on both axes.
| Parameters | |
|---|---|
int velocityX |
Fling velocity on the horizontal axis. |
int velocityY |
Fling velocity on the vertical axis. |
| Returns | |
|---|---|
int[] |
array holding the calculated distances in x and y directions respectively. |
findSnapView
public abstract @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 abstract 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 |
onFling
public boolean onFling(int velocityX, int velocityY)
Override this to handle a fling given the velocities in both x and y directions. Note that this method will only be called if the associated LayoutManager supports scrolling and the fling is not handled by nested scrolls first.
| Parameters | |
|---|---|
int velocityX |
the fling velocity on the X axis |
int velocityY |
the fling velocity on the Y axis |
| Returns | |
|---|---|
boolean |
true if the fling was handled, false otherwise. |
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 |
protected @Nullable LinearSmoothScrollercreateSnapScroller(@NonNull RecyclerView.LayoutManager layoutManager)
Creates a scroller to be used in the snapping implementation.
| Parameters | |
|---|---|
@NonNull RecyclerView.LayoutManager layoutManager |
The |
| Returns | |
|---|---|
@Nullable LinearSmoothScroller |
a |