Adding a RecyclerView

Before we move into the shared element transitions there are a few changes we need to change to our app as we want to reproduce the most common use of the shared element transitions, a RecyclerView with a details screen.

By the end of this step this will be our final app:

First of all we need to change the CardViewin both of our XML layout files to a RecyclerView.

Then we need to create the XML layout for our list items.

We then create a data class for our mock data:

On our MainActivity.kt we add the following methods

We also need to create a RecyclerView Adapter.

I'm going to use Glide library for setting the images to our ImageViews. Even though for this project we could use only thesetImageResource property, I find it a good practice to have a handy way of handling loading and errors (and unless you have the exact ID there's always a slight chance to get a NPE). And while you should also implement those, we don't have time to cover everything in this workshop.

To add Glide in your project add the following to your Gradle: implementation 'com.github.bumptech.glide:glide:4.8.0'

Finally we populate our RecyclerView with our dummy data

And of course we run our app to test that everything is working

Last updated