public class GravitySnapHelper
extends androidx.recyclerview.widget.LinearSnapHelper
LinearSnapHelper that allows snapping to an edge or to the center.
Possible snap positions:
Gravity.START, Gravity.TOP, Gravity.END, Gravity.BOTTOM,
Gravity.CENTER.
To customize the scroll duration, use setScrollMsPerInch(float).
To customize the maximum scroll distance during flings,
use setMaxFlingSizeFraction(float)
or setMaxFlingDistance(int)
| Modifier and Type | Class and Description |
|---|---|
static interface |
GravitySnapHelper.SnapListener
A listener that's called when the
RecyclerView used by GravitySnapHelper
changes its scroll state to RecyclerView.SCROLL_STATE_IDLE
and there's a valid snap position. |
| Modifier and Type | Field and Description |
|---|---|
static int |
FLING_DISTANCE_DISABLE |
static float |
FLING_SIZE_FRACTION_DISABLE |
| Constructor and Description |
|---|
GravitySnapHelper(int gravity) |
GravitySnapHelper(int gravity,
boolean enableSnapLastItem) |
GravitySnapHelper(int gravity,
boolean enableSnapLastItem,
GravitySnapHelper.SnapListener snapListener) |
GravitySnapHelper(int gravity,
GravitySnapHelper.SnapListener snapListener) |
| Modifier and Type | Method and Description |
|---|---|
void |
attachToRecyclerView(androidx.recyclerview.widget.RecyclerView recyclerView) |
int[] |
calculateDistanceToFinalSnap(androidx.recyclerview.widget.RecyclerView.LayoutManager layoutManager,
android.view.View targetView) |
int[] |
calculateScrollDistance(int velocityX,
int velocityY) |
androidx.recyclerview.widget.RecyclerView.SmoothScroller |
createScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager layoutManager) |
android.view.View |
findSnapView(androidx.recyclerview.widget.RecyclerView.LayoutManager lm) |
android.view.View |
findSnapView(androidx.recyclerview.widget.RecyclerView.LayoutManager lm,
boolean checkEdgeOfList) |
int |
getCurrentSnappedPosition() |
int |
getGravity()
Get the current gravity being applied
|
int |
getMaxFlingDistance() |
float |
getMaxFlingSizeFraction() |
float |
getScrollMsPerInch() |
boolean |
getSnapLastItem() |
boolean |
getSnapToPadding() |
boolean |
scrollToPosition(int position)
This method will only work if there's a ViewHolder for the given position.
|
void |
setGravity(int newGravity)
Changes the gravity of this
GravitySnapHelper
and dispatches a smooth scroll for the new snap position. |
void |
setGravity(int newGravity,
java.lang.Boolean smooth)
Changes the gravity of this
GravitySnapHelper
and dispatches a smooth scroll for the new snap position. |
void |
setMaxFlingDistance(int distance)
Changes the max fling distance in absolute values.
|
void |
setMaxFlingSizeFraction(float fraction)
Changes the max fling distance depending on the available size of the RecyclerView.
|
void |
setScrollMsPerInch(float ms)
Sets the scroll duration in ms per inch.
|
void |
setSnapLastItem(boolean snap)
Enable snapping of the last item that's snappable.
|
void |
setSnapListener(GravitySnapHelper.SnapListener listener)
Sets a
GravitySnapHelper.SnapListener to listen for snap events |
void |
setSnapToPadding(boolean snapToPadding)
If true, GravitySnapHelper will snap to the gravity edge
plus any amount of padding that was set in the RecyclerView.
|
boolean |
smoothScrollToPosition(int position)
Unlike
scrollToPosition(int),
this method will generally always find a snap view if the position is valid. |
void |
updateSnap(java.lang.Boolean smooth,
java.lang.Boolean checkEdgeOfList)
Updates the current view to be snapped
|
public static final int FLING_DISTANCE_DISABLE
public static final float FLING_SIZE_FRACTION_DISABLE
public GravitySnapHelper(int gravity)
public GravitySnapHelper(int gravity,
@NonNull
GravitySnapHelper.SnapListener snapListener)
public GravitySnapHelper(int gravity,
boolean enableSnapLastItem)
public GravitySnapHelper(int gravity,
boolean enableSnapLastItem,
@Nullable
GravitySnapHelper.SnapListener snapListener)
public void attachToRecyclerView(@Nullable
androidx.recyclerview.widget.RecyclerView recyclerView)
throws java.lang.IllegalStateException
attachToRecyclerView in class androidx.recyclerview.widget.SnapHelperjava.lang.IllegalStateException@Nullable
public android.view.View findSnapView(@NonNull
androidx.recyclerview.widget.RecyclerView.LayoutManager lm)
findSnapView in class androidx.recyclerview.widget.LinearSnapHelper@Nullable
public android.view.View findSnapView(@NonNull
androidx.recyclerview.widget.RecyclerView.LayoutManager lm,
boolean checkEdgeOfList)
@NonNull
public int[] calculateDistanceToFinalSnap(@NonNull
androidx.recyclerview.widget.RecyclerView.LayoutManager layoutManager,
@NonNull
android.view.View targetView)
calculateDistanceToFinalSnap in class androidx.recyclerview.widget.LinearSnapHelper@NonNull
public int[] calculateScrollDistance(int velocityX,
int velocityY)
calculateScrollDistance in class androidx.recyclerview.widget.SnapHelper@Nullable public androidx.recyclerview.widget.RecyclerView.SmoothScroller createScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager layoutManager)
createScroller in class androidx.recyclerview.widget.SnapHelperpublic void setSnapListener(@Nullable
GravitySnapHelper.SnapListener listener)
GravitySnapHelper.SnapListener to listen for snap eventslistener - a GravitySnapHelper.SnapListener that'll receive snap events or null to clear itpublic void setGravity(int newGravity,
java.lang.Boolean smooth)
GravitySnapHelper
and dispatches a smooth scroll for the new snap position.newGravity - one of the following: Gravity.START, Gravity.TOP,
Gravity.END, Gravity.BOTTOM, Gravity.CENTERsmooth - true if we should smooth scroll to new edge, false otherwisepublic void updateSnap(java.lang.Boolean smooth,
java.lang.Boolean checkEdgeOfList)
smooth - true if we should smooth scroll, false otherwisecheckEdgeOfList - true if we should check if we're at an edge of the list
and snap according to getSnapLastItem(),
or false to force snapping to the nearest viewpublic boolean scrollToPosition(int position)
public boolean smoothScrollToPosition(int position)
scrollToPosition(int),
this method will generally always find a snap view if the position is valid.
The smooth scroller from createScroller(RecyclerView.LayoutManager)
will be used, and so will scrollMsPerInch for the scroll velocity
public int getGravity()
Gravity.START, Gravity.TOP, Gravity.END,
Gravity.BOTTOM, Gravity.CENTERpublic void setGravity(int newGravity)
GravitySnapHelper
and dispatches a smooth scroll for the new snap position.newGravity - one of the following: Gravity.START, Gravity.TOP,
Gravity.END, Gravity.BOTTOM, Gravity.CENTERpublic boolean getSnapLastItem()
public void setSnapLastItem(boolean snap)
snap - true if you want to enable snapping of the last snappable itempublic int getMaxFlingDistance()
setMaxFlingDistance(int)
or FLING_DISTANCE_DISABLE if we're not limiting the fling distancepublic void setMaxFlingDistance(@Px
int distance)
distance - max fling distance in pixels
or FLING_DISTANCE_DISABLE
to disable fling limitspublic float getMaxFlingSizeFraction()
setMaxFlingSizeFraction(float)
or FLING_SIZE_FRACTION_DISABLE
if we're not limiting the fling distancepublic void setMaxFlingSizeFraction(float fraction)
Example: if you pass 0.5f and the RecyclerView measures 600dp, the max fling distance will be 300dp.
fraction - size fraction to be used for the max fling distance
or FLING_SIZE_FRACTION_DISABLE
to disable fling limitspublic float getScrollMsPerInch()
setScrollMsPerInch(float)
or 100fpublic void setScrollMsPerInch(float ms)
Default value is 100.0f
This value will be used in
createScroller(RecyclerView.LayoutManager)
ms - scroll duration in ms per inchpublic boolean getSnapToPadding()
public void setSnapToPadding(boolean snapToPadding)
The default value is false.
snapToPadding - true if you want to snap to the paddingpublic int getCurrentSnappedPosition()
RecyclerView.NO_POSITION in case there's none.