Fastest way for creating RecyclerView Adapter

Hello world.

First of all, this is my first blog post, so here is a quick intro. I’m will try to share some interesting blog posts about cool things that I know.

We all hate (or at least I do) writing boilerplate code so I would like to share this quick way for writing  RecyclerView Adapter. Before we create an adapter, we need to prepare 2 simple things; one is a class that will be our model, and second will be a layout for an item in the RecyclerView.

Now the fun stuff

Now go to Android Studio > Preferences > Editor > File and code templates and click on that “+” icon for adding the new one.

You may notice that I use ButterKnife library, but you don’t have to, but you should 🙂

Where is my adapter?

Now that we are all set, we need to finally create that adapter. Go to the File > New > and select your RecyclerView adapter template.

Screen Shot 2017-03-22 at 01.48.26

You will see a dialog like this, fill name for you adapter class, item class, and finally your item layout, click OK and… magic. The boring part of your adapter class is already written, all you have to do is make sure that you Inject your views and fill it with data (the fun part).

Published by

Kristijan Drača

Android developer since Android Gingerbread days. Except programming, he is interested in cryptocurrencies, DIY projects, games, etc.

5 thoughts on “Fastest way for creating RecyclerView Adapter”

  1. Butterknife requires a lot of private fields and annotations in your code, and it’s a heavy third party library. So you end up with more code to bug fix and test and a bigger app to ship to customers making their downloads longer and app slower.

    Databinding, on the other hand, has no third party library, since it is provided by Google. The fields are all generated for you in the binding class that is generated for your XML, and they match whatever views you gave IDs to in the XML. In Butterknife you put an ID attribute in your XML, then you put a field in your Java code, then you put an annotation in your Java code. Three things to write instead of one.

    You also rarely need to even give IDs to views since often they just have a databinding expression and never have to be accessed directly. You change the value they are binded to, and provided you used ObservableField or similar, it updates the view automatically without you ever having to access the view. So in a lot of cases it even removes the need for an ID attribute at all.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.