Composing attributes of a Dynamic RecyclerView with Functions

TJ
3 min readFeb 26, 2019

My two favorite Android ViewGroups are the ConstraintLayout, and the RecyclerView. Of those two however, only the latter has a boilerplate laden API. Typical use of a RecyclerView goes something thus:

rv.setAdapter(adapter);

rv.setLayoutManager(layoutManager);

rv.addOnScrollListener(new OnScrollListener() {…});

Worse still, if you wanted swipe or drag and drop behavior you could either:

  1. Subclass The ItemTouchHelper class and it’s verbose callbacks each and every time you wanted said behavior.
  2. Subclass the RecyclerView and add your own layer of abstraction for wrapping the ItemTouchHelper class and reuse that as often as you wish.

The latter is the oft used route, with numerous libraries on Github that offer it as a way to expedite your app development. If you’ve happened to stumble across any of my prior posts however, you’d notice I dislike inheritance as a medium (heh) of code re-usability; I’d much rather compose, delegating re-usability to functions or functional interfaces where possible.

With this motif described, I’d like to introduce the ListManager, a class whose sole purpose is to hold any disparate behaviors common to a RecyclerView and expose them in via a pleasant API. That’s half the story though. Most of the boiler plate in RecyclerView use comes from actually creating the Adapters, LayoutManagers and the like. So the ListManagerBuilder class and its parent AbstractListManagerBuilder offer fluent API’s for creating any generic type-safe RecyclerView, and functions for adding common behavior like applying DiffResults, swiping, dragging and dropping and so on.

Enough prose! What does use of the API look like? Well I’m glad you asked:

ListManager Construction with Swipe and Drag and Drop functionality
Swipe and Drag and Drop ListManager Demo

All behaviors this RecyclerView is displaying, are provided by functions, making it easy to reuse this pattern multiple times over. Have an endless scrolling list? Its got you covered:

Endless scrolling with a ListManager
Endless Scrolling Demo

So how does this all work? It uses the RecyclerView APIs already exposed, but simply wraps them and proxies calls to the functions supplied to the builder. This makes it pretty agnostic to whatever it is you’re building, all you need do is supply the appropriate function when necessary.

The Builder APIs are also open. If you needed to use a LayoutManager that isn’t provided out the box say the FlexBoxLayoutManager from Google, you can easily override the buildLayoutManager() method and return the appropriate LayoutManager instance you prefer. There’s also a convenience method for acting on inbuilt LayoutManager’s if you want to set a dynamic span count and the like called onLayoutManager(Consumer<LayoutManager>).

The source code for the demo screens an be found here:

If you’d like to try it out yourself, add the following to your app dependencies:

implementation 'com.tunjid.android-bootstrap:recyclerview:5.0.0-alpha3'

That’s it, happy RecyclerView building!

--

--