A SnapHelper that snaps a RecyclerView to an edge.
Add this to your build.gradle:
implementation'com.github.rubensousa:gravitysnaphelper:2.2.2'
You can either create a GravitySnapHelper, or use GravitySnapRecyclerView.
If you want to use GravitySnapHelper directly,you just need to create it and attach it to your RecyclerView:
val snapHelper=GravitySnapHelper(Gravity.START)snapHelper.attachToRecyclerView(recyclerView)
If you want to use GravitySnapRecyclerView, you can use the following xml attributes for customisation:
<attrname="snapGravity"format="enum"><attrname="snapEnabled"format="boolean" /><attrname="snapLastItem"format="boolean" /><attrname="snapToPadding"format="boolean" /><attrname="snapScrollMsPerInch"format="float" /><attrname="snapMaxFlingSizeFraction"format="float" />
Example:
<com.github.rubensousa.gravitysnaphelper.GravitySnapRecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="wrap_content"app:snapGravity="start" />
val snapHelper=GravitySnapHelper(Gravity.START)snapHelper.attachToRecyclerView(recyclerView)

val snapHelper=GravitySnapHelper(Gravity.CENTER)snapHelper.attachToRecyclerView(recyclerView)

If you usesetMaxFlingSizeFraction orsetMaxFlingDistanceyou can change the maximum fling distance allowed.


- setMaxFlingDistance orsetMaxFlingSizeFraction - changes the max fling distance allowed.
- setScrollMsPerInch - changes the scroll speed.
- setGravity - changes the gravity of the SnapHelper.
- setSnapToPadding - enables snapping to padding (default is false)
- smoothScrollToPosition andscrollToPosition
- RTL support out of the box
Take a look at these blog posts if you're using nested RecyclerViews
Improving scrolling behavior of nested RecyclerViews
Saving scroll state of nested RecyclerViews
Copyright 2018 The Android Open Source ProjectCopyright 2019 Rúben SousaLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.