The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


RecyclerView

Visually representing collections of items is an important aspect of many mobile apps. The classic Android implementation of this was the AdapterView family of widgets: ListView, GridView, Spinner, and so on. However, they had their limitations, particularly with respect to advanced capabilities like animating changes in the list contents.

In 2014, Google released RecyclerView, via the Android Support package. Developers can add the recyclerview-v7 to their projects and use RecyclerView as a replacement for most of the AdapterView family. RecyclerView was written from the ground up to be a more flexible container, with lots of hooks and delegation to allow behaviors to be plugged in.

This had two major impacts:

  1. RecyclerView is indeed much more powerful than its AdapterView counterparts
  2. RecyclerView, out of the box, is nearly useless, and wiring together enough stuff to even replicate basic ListView/GridView functionality takes quite a bit of code

In this chapter, we will review RecyclerView from the ground up, starting with basic operation. Many of the ListView samples from elsewhere in the book will be replicated here, to see how to pull off the same things with RecyclerView. And, we will explore some of the additional capabilities that make RecyclerView perhaps worth the effort on high-end Android applications.

Prerequisites

Understanding this chapter requires that you have read the core chapters, particularly the one on AdapterView and adapters.

One section involves the use of custom XML drawables. Another section demonstrates using content pulled from the MediaStore ContentProvider.

This chapter also covers things like action modes and other advanced ListView techniques.

AdapterView and its Discontents

AdapterView, and particularly its ListView and GridView subclasses, serve important roles in Android application development. And, for basic scenarios, they work reasonably well.

However, there are issues.

Perhaps the biggest tactical issue is that updating an AdapterView tends to be an all-or-nothing affair. If there is a change to the model data — new rows added, existing rows removed, or data changes that might affect the AdapterView presentation — the only well-supported solution is to call notifyDataSetChanged() and have the AdapterView rebuild itself. This is slow and can have impacts on things like choice states. And, if you wanted to get really elaborate about your changes, and use animated effects to show where rows got added or removed, that was halfway to impossible.

Strategically, AdapterView, AbsListView (the immediate parent of ListView and GridView), and ListView are large piles of code that resemble pasta to many outsiders. There are so many responsibilities piled into these classes that maintainability was a challenge for Google and extensibility was a dream more than a reality.

Enter RecyclerView

The preview of this section is in the process of being translated from its native Klingon.

A Trivial List

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

Divider Options

The preview of this section apparently resembled a Pokémon.

Handling Click Events

The preview of this section is presently indisposed.

What About Cursors?

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Grids

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Varying the Items

The preview of this section will not appear here for a while, due to a time machine mishap.

Mutable Row Contents

The preview of this section is [REDACTED].

Changing the Contents

The preview of this section was lost in the sofa cushions.

The Order of Things

The preview of this section was abducted by space aliens.

Other Bits of Goodness

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Animating the Deltas Using DiffUtil

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

The March of the Libraries

The preview of this section was eaten by a grue.