Drawables / RecyclerView / Shape Drawable

RecyclerView FastScroll – Part 1

In the previous series we looked at how we could get smoothScrollToPosition() working in a timely manner when using LinearLayoutManager with a large data set. An alternative UX approach to using smoothScrollToPosition() which may be applicable in some use-cases is to avoid smooth scrolling altogether and use the fast scroll behaviour which has long been a staple of ListView. However, RecyclerView does not have fast scroll built in so, in this short series, we’ll look at how to implement fast scroll in RecyclerView.

So the behaviour that we’re after is a normal scroll bar behaviour with a ‘bubble’ which moves along with the RecyclerView scrolling, but when the user drags the bubble, a draggable handle pops up which hides after a short delay when the user ends the drag:

We’ll take the code from the previous series and add fast scroll behaviour to it. The code for this series will be minSdkVersion='21' to simplify the drawables required (and keep the code a little cleaner). It will be easy to port this code to much earlier versions by adapting the drawables accordingly but little or no other changes will be required.

Let’s start by defining the drawable in question. This will be the drag handle which will appear when fast scrolling is active. I have copied the general appearance from the Contacts app fast scroller, its a rectangle with all but the bottom right corner rounded, and we’ll pick up the accent colour:

We’re using a shape drawable here, so in Lollipop we’ll automatically get the correct shadow if we apply elevation. The shadow is the main simplification we get by using minSdkVersion='21'. To back port this code you’ll need to use an alternate approach here, such as using a bitmap drawable here instead of a shape drawable.

Our FastScroller control will also provide its own scroll ‘bubble’ – the bubble showing the current scroll position that would normally be displayed with the RecyclerView itself. Once again this is a simple shape drawable which pick up a primary colour from the theme:

This may need a slight tweak of ?android:attr/colorPrimaryDark to get it to work with AppCompat themes.

Next we need to define a layout which will contain the track and handle, with the handle initially invisible:

Next we’ll create a custom view which will encapsulate our fast scroll behaviour:

This is a based upon a horizontal LinearLayout which will provide the fast scroll behaviour for a RecyclerView instance which scrolls vertically (the orientation is hard-coded, but it should be easy enough to adapt this, if necessary). We need to call setClipChildren(false) otherwise our elevation shadows may get clipped. We need to extend LinearLayout rather than, for example FrameLayout, here because the fastscroller.xml layout contains two controls which need to be positioned side-by-side, we want to keep the layout hierarchy as flat as possible so we don’t want an additional layout level in there so we use as the parent of that layout which requires our custom control to extend LinearLayout to correctly position these two Views.

Next we’ll add a couple of methods with will create the animations to show and hide the handle:

These are both pretty straightforward – we just grow and shrink from the bottom right corner while fading in and out.

We also need to know the track length for knowing where to position the bubble, and also determining the item we need to scroll to when we receive a touch event. The easiest way to do this is override onSizeChanged, and get the height whenever it’s called:

That’s the basis of our FastScroller, but we now need to handle touch events in order to allow the user to fast scroll, and also track changes in the RecyclerView scrolling so that the bubble position responds to the user directly scrolling the RecyclerView. We’ll look at implementing those in the concluding article in this series.

Normally I like to publish code along with each article, but the above code in isolation doesn’t actually do very much, so in this case I’ll hold it back until the next article when everything will be connected up and working.

© 2015, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 RecyclerView FastScroll – Part 1 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *