LinearSnapHelper


public class LinearSnapHelper extends SnapHelper


Implementation of the SnapHelper supporting snapping in either vertical or horizontal orientation.

The implementation will snap the center of the target child view to the center of the attached RecyclerView. If you intend to change this behavior then override calculateDistanceToFinalSnap.

Summary

Public constructors

Public methods

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.

View

Override this method to provide a particular target view for snapping.

int
findTargetSnapPosition(
    RecyclerView.LayoutManager layoutManager,
    int velocityX,
    int velocityY
)

Override to provide a particular adapter target position for snapping.

Inherited methods

From androidx.recyclerview.widget.SnapHelper
void

Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener.

int[]
calculateScrollDistance(int velocityX, int velocityY)

Calculated the estimated scroll distance in each direction given velocities on both axes.

@Nullable RecyclerView.SmoothScroller

Creates a scroller to be used in the snapping implementation.

@Nullable LinearSmoothScroller

This method is deprecated.

use createScroller instead.

boolean
onFling(int velocityX, int velocityY)

Override this to handle a fling given the velocities in both x and y directions.

Public constructors

LinearSnapHelper

Added in 1.0.0
public LinearSnapHelper()

Public methods

calculateDistanceToFinalSnap

Added in 1.4.0-rc01
public 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 RecyclerView.LayoutManager associated with the attached RecyclerView

@NonNull View targetView

the target view that is chosen as the view to snap

Returns
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

Added in 1.4.0-rc01
public 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 RecyclerView.LayoutManager associated with the attached RecyclerView

Returns
View

the target view to which to snap on fling or end of scroll

findTargetSnapPosition

Added in 1.4.0-rc01
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 RecyclerView.LayoutManager associated with the attached RecyclerView

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 NO_POSITION if no snapping should happen