-
public abstract class SnapHelper extends RecyclerView.OnFlingListener
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.
-
-
Method Summary
Modifier and Type Method Description boolean
onFling(int velocityX, int velocityY)
Override this to handle a fling given the velocities in both x and y directions. void
attachToRecyclerView(@Nullable() RecyclerView recyclerView)
Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener. Array<int>
calculateScrollDistance(int velocityX, int velocityY)
Calculated the estimated scroll distance in each direction given velocities on both axes. abstract Array<int>
calculateDistanceToFinalSnap(@NonNull() RecyclerView.LayoutManager layoutManager, @NonNull() View targetView)
Override this method to snap to a particular point within the target view or the containerview on any axis. abstract View
findSnapView(RecyclerView.LayoutManager layoutManager)
Override this method to provide a particular target view for snapping. abstract int
findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY)
Override to provide a particular adapter target position for snapping. -
-
Method Detail
-
onFling
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:
velocityX
- the fling velocity on the X axisvelocityY
- the fling velocity on the Y axis
-
attachToRecyclerView
void attachToRecyclerView(@Nullable() RecyclerView recyclerView)
Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener.You can call this method with
{@code null}
to detach it from the current RecyclerView.- Parameters:
recyclerView
- The RecyclerView instance to which you want to add this helper or{@code null}
if you want to remove SnapHelper from the currentRecyclerView.
-
calculateScrollDistance
Array<int> calculateScrollDistance(int velocityX, int velocityY)
Calculated the estimated scroll distance in each direction given velocities on both axes.
- Parameters:
velocityX
- Fling velocity on the horizontal axis.velocityY
- Fling velocity on the vertical axis.
-
calculateDistanceToFinalSnap
@Nullable() abstract Array<int> calculateDistanceToFinalSnap(@NonNull() RecyclerView.LayoutManager layoutManager, @NonNull() View targetView)
Override this method to snap to a particular point within the target view or the containerview on any axis.
This method is called when the SnapHelper has intercepted a fling and it needsto know the exact distance required to scroll by in order to snap to the target view.
- Parameters:
layoutManager
- the RecyclerView.LayoutManager associated with the attachedRecyclerViewtargetView
- the target view that is chosen as the view to snap
-
findSnapView
@Nullable() abstract 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 requiresa target view to snap to. It will be explicitly called when the scroll state becomes idleafter a scroll. It will also be called when the SnapHelper is preparing to snapafter a fling and requires a reference view from the current set of child views.
If this method returns
{@code null}
, SnapHelper will not snap to any view.- Parameters:
layoutManager
- the RecyclerView.LayoutManager associated with the attachedRecyclerView
-
findTargetSnapPosition
abstract int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY)
Override to provide a particular adapter target position for snapping.
- Parameters:
layoutManager
- the RecyclerView.LayoutManager associated with the attachedRecyclerViewvelocityX
- fling velocity on the horizontal axisvelocityY
- fling velocity on the vertical axis
-
-
-
-