belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1
SnapHelper
public
abstract
class
SnapHelper
extends RecyclerView.OnFlingListener
java.lang.Object | ||
↳ | android.support.v7.widget.RecyclerView.OnFlingListener | |
↳ | android.support.v7.widget.SnapHelper |
Known Direct Subclasses |
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(int, int)
and handle fling manually.
Summary
Public constructors | |
---|---|
SnapHelper()
|
Public methods | |
---|---|
void
|
attachToRecyclerView(RecyclerView recyclerView)
Attaches the |
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. |
Protected methods | |
---|---|
RecyclerView.SmoothScroller
|
createScroller(RecyclerView.LayoutManager layoutManager)
Creates a scroller to be used in the snapping implementation. |
LinearSmoothScroller
|
createSnapScroller(RecyclerView.LayoutManager layoutManager)
This method was deprecated
in API level 26.1.0.
use |
Inherited methods | |
---|---|
From
class
android.support.v7.widget.RecyclerView.OnFlingListener
| |
From
class
java.lang.Object
|
Public constructors
Public methods
attachToRecyclerView
void attachToRecyclerView (RecyclerView recyclerView)
Attaches the SnapHelper
to the provided RecyclerView, by calling
setOnFlingListener(RecyclerView.OnFlingListener)
.
You can call this method with null
to detach it from the current RecyclerView.
Parameters | |
---|---|
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. |
Throws | |
---|---|
IllegalArgumentException |
if there is already a RecyclerView.OnFlingListener
attached to the provided RecyclerView .
|
IllegalStateException |
calculateDistanceToFinalSnap
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.
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 | |
---|---|
layoutManager |
RecyclerView.LayoutManager : the RecyclerView.LayoutManager associated with the attached
RecyclerView |
targetView |
View : 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. |
calculateScrollDistance
int[] calculateScrollDistance (int velocityX, int velocityY)
Calculated the estimated scroll distance in each direction given velocities on both axes.
Parameters | |
---|---|
velocityX |
int : Fling velocity on the horizontal axis. |
velocityY |
int : Fling velocity on the vertical axis. |
Returns | |
---|---|
int[] |
array holding the calculated distances in x and y directions respectively. |
findSnapView
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 | |
---|---|
layoutManager |
RecyclerView.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
int findTargetSnapPosition (RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY)
Override to provide a particular adapter target position for snapping.
Parameters | |
---|---|
layoutManager |
RecyclerView.LayoutManager : the RecyclerView.LayoutManager associated with the attached
RecyclerView |
velocityX |
int : fling velocity on the horizontal axis |
velocityY |
int : 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
|
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 RecyclerView.LayoutManager
supports scrolling and the fling is not handled by nested scrolls first.
Parameters | |
---|---|
velocityX |
int : the fling velocity on the X axis |
velocityY |
int : the fling velocity on the Y axis |
Returns | |
---|---|
boolean |
true if the fling was handled, false otherwise. |
Protected methods
createScroller
RecyclerView.SmoothScroller createScroller (RecyclerView.LayoutManager layoutManager)
Creates a scroller to be used in the snapping implementation.
Parameters | |
---|---|
layoutManager |
RecyclerView.LayoutManager : The RecyclerView.LayoutManager associated with the attached
RecyclerView . |
Returns | |
---|---|
RecyclerView.SmoothScroller |
a RecyclerView.SmoothScroller which will handle the scrolling.
|
createSnapScroller
LinearSmoothScroller createSnapScroller (RecyclerView.LayoutManager layoutManager)
This method was deprecated
in API level 26.1.0.
use createScroller(LayoutManager)
instead.
Creates a scroller to be used in the snapping implementation.
Parameters | |
---|---|
layoutManager |
RecyclerView.LayoutManager : The RecyclerView.LayoutManager associated with the attached
RecyclerView . |
Returns | |
---|---|
LinearSmoothScroller |
a LinearSmoothScroller which will handle the scrolling. |
Annotations
Interfaces
- ActionMenuView.OnMenuItemClickListener
- PopupMenu.OnDismissListener
- PopupMenu.OnMenuItemClickListener
- RecyclerView.ChildDrawingOrderCallback
- RecyclerView.ItemAnimator.ItemAnimatorFinishedListener
- RecyclerView.LayoutManager.LayoutPrefetchRegistry
- RecyclerView.OnChildAttachStateChangeListener
- RecyclerView.OnItemTouchListener
- RecyclerView.RecyclerListener
- RecyclerView.SmoothScroller.ScrollVectorProvider
- SearchView.OnCloseListener
- SearchView.OnQueryTextListener
- SearchView.OnSuggestionListener
- ShareActionProvider.OnShareTargetSelectedListener
- ThemedSpinnerAdapter
- Toolbar.OnMenuItemClickListener
Classes
- ActionMenuView
- ActionMenuView.LayoutParams
- AppCompatAutoCompleteTextView
- AppCompatButton
- AppCompatCheckBox
- AppCompatCheckedTextView
- AppCompatEditText
- AppCompatImageButton
- AppCompatImageView
- AppCompatMultiAutoCompleteTextView
- AppCompatRadioButton
- AppCompatRatingBar
- AppCompatSeekBar
- AppCompatSpinner
- AppCompatTextView
- CardView
- DefaultItemAnimator
- DividerItemDecoration
- GridLayout
- GridLayout.Alignment
- GridLayout.LayoutParams
- GridLayout.Spec
- GridLayoutManager
- GridLayoutManager.DefaultSpanSizeLookup
- GridLayoutManager.LayoutParams
- GridLayoutManager.SpanSizeLookup
- LinearLayoutCompat
- LinearLayoutCompat.LayoutParams
- LinearLayoutManager
- LinearLayoutManager.LayoutChunkResult
- LinearSmoothScroller
- LinearSnapHelper
- ListPopupWindow
- OrientationHelper
- PagerSnapHelper
- PopupMenu
- RecyclerView
- RecyclerView.Adapter
- RecyclerView.AdapterDataObserver
- RecyclerView.EdgeEffectFactory
- RecyclerView.ItemAnimator
- RecyclerView.ItemAnimator.ItemHolderInfo
- RecyclerView.ItemDecoration
- RecyclerView.LayoutManager
- RecyclerView.LayoutManager.Properties
- RecyclerView.LayoutParams
- RecyclerView.OnFlingListener
- RecyclerView.OnScrollListener
- RecyclerView.RecycledViewPool
- RecyclerView.Recycler
- RecyclerView.SimpleOnItemTouchListener
- RecyclerView.SmoothScroller
- RecyclerView.SmoothScroller.Action
- RecyclerView.State
- RecyclerView.ViewCacheExtension
- RecyclerView.ViewHolder
- RecyclerViewAccessibilityDelegate
- RecyclerViewAccessibilityDelegate.ItemDelegate
- SearchView
- ShareActionProvider
- SimpleItemAnimator
- SnapHelper
- StaggeredGridLayoutManager
- StaggeredGridLayoutManager.LayoutParams
- SwitchCompat
- ThemedSpinnerAdapter.Helper
- Toolbar
- Toolbar.LayoutParams
- Toolbar.SavedState
- TooltipCompat