A Very Mature RecyclerView

A recyclerview is said to be mature when it is written with clean or modularized code, handling clicks in the activity rather than in the adapter.

Tobiloba Adejumo
MindOrks

--

Vous êtes les bienvenus, Android Engineers!

A short tutorial that explains how to write a mature recyclerview.

What is a mature recyclerview?

A mature recyclerview is one that allows the handling of items in the activity rather than the adapter.

Here’s a sample app to show this…

The source code can be found here. The app contains the names of some Android GDE’s. When an item is clicked, it triggers a toast message for that particular item. This is handled in the activity rather than in the adapter.

How can I accomplish this?

  1. Create a POJO class

2. Create an Interface.

3. Remember to include your source of data, be in network or not.

4 . Prepare your Adapter 👉

Things to take note of in the adapter…

  1. bind() method in MyViewHolderClass. An onClickListener is implemented there such that the object of the interface takes the model object to be used in the calling activity.
  2. onBindViewHolder() method in Adapter Class: The bind() method is called here taking the arguments model, i.e the object of the Model List at that particular point, and the object of my interface class created initially.

Also note that, the adapter’s constructor contains the object of in the defined interface as a parameter.

Finally, do this to your activity.

Create a setAdapter() method

And in your onCreate overriden method?

Oh yeah, it’s working! 😄

--

--

Tobiloba Adejumo
MindOrks

Interested in biomarker development, software dev and ai, as well as psychology, history, philosophy, relationships. Website: tobilobaadejumo.com