Background
On ListView, you could have a fast scroller which allowed you to drag a scrollbar to easily scroll to wherever you wished (using fastScrollEnabled attribute)
Together with "SectionIndexer" class and optionally some attributes, you could have a nice popup that shows as you use this scrollbar (link here).
Such a thing is shown on the contacts app so that you could scroll easily to specific letters.
The problem
RecyclerView doesn't seem to have any of those. Not even a fast-scroll.
The question
How do I add a fast scroller functionality for the RecyclerView?
Just enable fast scrolling and add thumb, tracker for scroll bar like below.
There are a lot of unanswered questions about
RecyclerView
and fast-scroll/section indexer, let's try to regroup and gather our opinions and information here.The short answer is: NO, you can't enable the fast-scroll because RecyclerView does not contain a FastScroller object and nor any related logical-state variables. This because
RecyclerView
is not an AbsListView.On the other hand, it's not impossible to implement a
RecyclerView
which contains a dumped version ofFastScroller
and the necessary logic for the fast-scrolling, but I have not seen any implementation of this so far.Please share your consideration about that or if you think I'm wrong.
The Android Support Library 26.0.0 now supports
fastScrollEnabled
New fastScrollEnabled boolean flag for RecyclerView.
If enabled, fastScrollHorizontalThumbDrawable, fastScrollHorizontalTrackDrawable, fastScrollVerticalThumbDrawable, and fastScrollVerticalTrackDrawable must be set.
Sample - https://android.jlelse.eu/fast-scrolling-with-recyclerview-2b89d4574688
There is a provision of implementing scroll bars with
RecycleView
and itsLayoutManager
.For example:
computeVerticalScrollExtent()
,computeVerticalScrollOffset()
andcomputeVerticalScrollRange()
can provide information for always positioning a vertical scroll thumb at correct place.These methods are also there in
LayoutManager
for delegating actual measurements. So theLayoutManager
implementation used must support these measurements.Also, drag touch on scroll thumb can be intercepted by overriding
onInterceptTouchEvent()
ofRecyclerView
. And after calculating the desired scroll,scrollTo()
can be called to updateRecyclerView
.Since all third party libraries had issues, I've decided to gather what I can find (mostly from here), fix everything and publish my own POC of the fast-scroller of the RecyclerView :
https://github.com/AndroidDeveloperLB/LollipopContactsRecyclerViewFastScroller
usage:
make a RecyclerView.Adapter that implements BubbleTextGetter, which given a position in the data will return the text to show in the bubble-popup.
position the FastScroller inside the layout that container the RecyclerView (probably at the right area).
Customize the FastScroller FastScroller
Some disadvantages:
Code:
BubbleTextGetter
recycler_view_fast_scroller__fast_scroller.xml
MainActivity
FastScroller
I stumbled on this question a few days ago when I ran into this situation. Here is my example implementation of FastScroll for RecyclerView:
github.com/danoz73/RecyclerViewFastScroller
Try running the example application, and peruse the code to see a fairly simple usage of a simple RecyclerViewFastScroller widget. There is information on github, but I'll include the basic usage here for a vertical fast scroller.
For a full example, refer to the sample application in the repo.
Basic Usage
In the activity or fragment XML where your RecyclerView resides, include a VerticalRecyclerViewFastScroller object. The following example would be in a relative layout:
In your fragment or activity where you setup layout programmatically, hook up the fast scroller to the recycler:
Hope this helps!
EDIT: There is now added support for Android-Lollipop-Contacts-style section indicators! Check out the sample application's implementation for details.