In ListView, you could have a fast scroller which allowed you to drag a scrollbar to easily scroll to wherever you wished using fastScrollEnabled
attribute. With Support Library 26, you can also easily enable fast scrolling for RecyclerView.
Add Dependency
Support Library 26 has now been moved to Google’s maven repository, first include that in your project level build.gradle.
buildscript { repositories { google() } .... }
Add Support Library 26 in your app level build.gradle.
dependencies { .... compile 'com.android.support:design:26.0.2' compile 'com.android.support:recyclerview-v7:26.0.2' .... }
Enable Fast Scrolling
If fastScrollEnabled
boolean flag for RecyclerView is enabled then,fastScrollHorizontalThumbDrawable
,fastScrollHorizontalTrackDrawable
, fastScrollVerticalThumbDrawable
, and fastScrollVerticalTrackDrawable
must be set.
- fastScrollEnabled : Setting this as true will require that must provide the following four properties.
- fastScrollHorizontalThumbDrawable : StateListDrawable used for drawing the thumb which will be draggable across the horizontal axis.
- fastScrollHorizontalTrackDrawable : StateListDrawable used for drawing the line that will represent the scrollbar on the horizontal axis.
- fastScrollVerticalThumbDrawable : StateListDrawable used for drawing the thumb which will be draggable on the vertical axis.
- fastScrollVerticalTrackDrawable : StateListDrawable used for draw the line that will represent the scrollbar on the vertical axis.
- Now, Create native shapes StateListDrawables. line.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/darker_gray" /> <padding android:top="10dp" android:left="10dp" android:right="10dp" android:bottom="10dp"/> </shape>
line_drawable.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/line"/> <item android:drawable="@drawable/line"/> </selector>
thumb.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:topLeftRadius="44dp" android:topRightRadius="44dp" android:bottomLeftRadius="44dp" /> <padding android:paddingLeft="22dp" android:paddingRight="22dp" /> <solid android:color="@color/colorPrimaryDark" /> </shape>
thumb_drawable.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/thumb"/> <item android:drawable="@drawable/thumb"/> </selector>
Now Create RecyclerView
and enable fastScrollEnabled
.
<android.support.v7.widget.RecyclerView .... app:fastScrollEnabled="true" app:fastScrollHorizontalThumbDrawable="@drawable/thumb_drawable" app:fastScrollHorizontalTrackDrawable="@drawable/line_drawable" app:fastScrollVerticalThumbDrawable="@drawable/thumb_drawable" app:fastScrollVerticalTrackDrawable="@drawable/line_drawable" />