public abstract class SnapHelper extends RecyclerView.OnFlingListener
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(int, int)
and handle fling manually.
Constructor and Description |
---|
SnapHelper() |
Modifier and Type | Method and Description |
---|---|
void |
attachToRecyclerView(RecyclerView recyclerView)
Attaches the
SnapHelper to the provided RecyclerView, by calling
RecyclerView.setOnFlingListener(RecyclerView.OnFlingListener) . |
abstract int[] |
calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager,
View targetView)
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 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.
|
boolean |
onFling(int velocityX,
int velocityY)
Override this to handle a fling given the velocities in both x and y directions.
|
public boolean onFling(int velocityX, int velocityY)
RecyclerView.OnFlingListener
RecyclerView.LayoutManager
supports scrolling and the fling is not handled by nested scrolls first.onFling
in class RecyclerView.OnFlingListener
velocityX
- the fling velocity on the X axisvelocityY
- the fling velocity on the Y axispublic void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException
SnapHelper
to the provided RecyclerView, by calling
RecyclerView.setOnFlingListener(RecyclerView.OnFlingListener)
.
You can call this method with null
to detach it from the current RecyclerView.recyclerView
- The RecyclerView instance to which you want to add this helper or
null
if you want to remove SnapHelper from the current
RecyclerView.IllegalArgumentException
- if there is already a RecyclerView.OnFlingListener
attached to the provided RecyclerView
.IllegalStateException
public int[] calculateScrollDistance(int velocityX, int velocityY)
velocityX
- Fling velocity on the horizontal axis.velocityY
- Fling velocity on the vertical axis.@Nullable public abstract int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView)
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.
layoutManager
- the RecyclerView.LayoutManager
associated with the attached
RecyclerView
targetView
- the target view that is chosen as the view to snap@Nullable public abstract View findSnapView(RecyclerView.LayoutManager layoutManager)
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.
layoutManager
- the RecyclerView.LayoutManager
associated with the attached
RecyclerView
public abstract int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY)
layoutManager
- the RecyclerView.LayoutManager
associated with the attached
RecyclerView
velocityX
- fling velocity on the horizontal axisvelocityY
- fling velocity on the vertical axisRecyclerView.NO_POSITION
if no snapping should happen