An interesting way to see how Kotlin simplifies your life is by creating a RecyclerView Adapter.

You’ll see that the code can be organized in such a way that its reading is easier and it avoids redundant code.

RecyclerView Adapter in Kotlin

We’ll create an Adapter that will set a title and an image into a cell.

It’ll be a very simple adapter in which we won’t allow the modification of the items. If we want new data, we will create a new adapter and set it to the RecyclerView.

The model

We’ll use a very simple model too, which only needs an identifier, the title and the url of the image.

We’ll use a data class, which if you remember we saw a few articles ago:

With this we already have a class with its constructor, its immutable properties, and other useful functions like equals or hashCode implemented.

The Adapter

The Adapter’s structure would be the following, self-generating the necessary methods:

You’ll see that I’ve created a ViewHolder class, which extends from the original.

This is because the Adapter needs an implementation of the original abstract class.

Also, there are some elements marked as nullable. This is because if the library isn’t properly annotated with the annotations @Nullable and @NonNull, Kotlin has no way of knowing if nulls are allowed, and it lets us decide.

If we self-generate the methods, by default it’ll take for granted that the values are nullable.

But studying the support library a bit, we know that those values aren’t null, so we can remove it:

The constructor

The adapter needs to receive the items and a listener by parameter. The result is something like this:

The implementation of the methods is very easy. I’m using an extension function that I’d created in a previous article to inflate the view:

There are three methods can be implemented with the contracted form, obtaining the previous result. In three lines we have all the adapter implemented.

Now go for the ViewHolder implementation.

The ViewHolder

The ViewHolder will assign the values from the model to their corresponding views:

Everything we have here has already been seen in other articles: the with function, the loadUrl extension function for ImageView, the access to Views using Kotlin Android Extensions and the mapping of the click listener.

As mentioned by Nabil in the comments, this way to use Kotlin Android Extensions in Views has some hidden costs, and is not always optimal. Take a look at the Kotlin Android Extensions article in this blog, where I show a new way to use cache on ViewHolders, and also this other article which talks about this hidden costs I’m mentioning. In any case, my recommendation is to test before optimizing.

Adapter assignment

There’s only one thing left: assign the adapter to the view

The final function is listener, which receives an item. The code will simply print the title of the item you click on.

Conclusion

As simple as that is to implement a RecyclerView Adapter in Kotlin.

Using a few of the tools we have learned so far, we’ve simplified the code to the minimum.

If you want to learn a lot more about all this and get enough fluency to create your own Android Apps, I recommend that you get the free guide to learn how to build your first project, or just get the book and learn how to create a complete App from scratch.

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.

Shares
%d bloggers like this: