Generic RecyclerViewAdapter for Android
--
Most of the time when I’m developing an Android app for my company 9to5 App Development, at some point I need a RecyclerView showing an list or grid of items of one custom class.
While doing this it started to annoy me that I had to write the same code over and over again. To prevent this and to add as many RecyclerViews as I want within a matter of seconds, I created a Generic RecyclerViewAdapter. This post explains how to do this when you are not using databinding. This has been done multiple times before me, but this is my take on it.
My colleague Tom Wolters has done the same thing, but for a UITableView in iOS development. Check out his article:
In my article I explain my default setup. First I will show the basics such as the the layout xml and the data model.
Data model
As an example I will assume you want to display a list of pets in a RecyclerView. Introducing, the Pet
class:
public class Pet {
private String name;
private String pictureUrl;
}
The Adapter
This is where the magic happens. I will first show you the code:
The constructor takes three parameters. The first is the dataset; an array with the generic type, in our case the array with pets. The second parameter is an interface named OnRecyclerViewItemClickListener which will be called when an item in the RecyclerView has been clicked. The third parameter is a layoutId, which points to the layout xml that describes how an item will be presented in the RecyclerView.
In the onCreateViewHolder
method the view for an item is inflated with the given layoutId and this is stored in the ViewHolder. The ViewHolder
is a class defined on line 38 – 45. This is stores a reference to the inflated View, so it can be recycled by the RecyclerView. As you can see this View is a RecyclerViewRow
which is a interface defined by me:
It is nothing but a method which the View of an item in a RecyclerView should contain to be sure the data is received and presented in this View.
Back to the RecyclerViewAdapter: The onBindViewHolder
method is called when the view should be filled at a certain position. We get the ViewHolder and the position. We use the position to select the correct pet from our dataset and call the showData
method with this pet as a parameter.
For our pets the View implementation would look something like this:
Where you can see the implementation of showData
. To make sure this class PetRow
is inflated along with the given layoutId, you should form the xml as follows:
The important part here is com.example.app.rows.PetRow
where com.example.app
is your package name. This part ensures the inflation of the PetRow
class which implements the RecyclerViewRow
interface with the showData
method.
Using the adapter
Finally, to use this adapter, you can simply use:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);RecyclerViewAdapter<Pet> adapter = new RecyclerViewAdapter<>(pets, this, R.layout.row_pet);recyclerView.setAdapter(adapter)
Where pets
is your array of pets.
When you want to use this adapter for a different RecyclerView with a different class, let’s say Plant
, you simply create an xml file row_plant.xml
with a reference to a PlantRow
View class that implements the RecyclerViewRow
interface and reuse the generic table view adapter again:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);RecyclerViewAdapter<Plant> adapter = new RecyclerViewAdapter<>(plants, this, R.layout.row_plant);recyclerView.setAdapter(adapter)
That’s it! If you have any questions, just ask!
Susan Pesman
I am an entrepreneur and Android / iOS developer at my company 9to5 App Development. Are you in need of a native app or just interested in our work? Check out our website: https://www.9to5.software