Android RecyclerView with Kotlin – Part 2

In part one of this series, we set up the basic RecyclerView structure. In this the second part, we will finish creating the adapter. Let’s begin!

First, make ViewHolder extend the correct class, RecyclerView.ViewHolder, and pass in the view from the ViewHolder class. Your code should now look like this:

If you look closely, you will see the Android Studio is throwing an error under class UsersAdapter, as indicated by a red line under class UsersAdapter. Let’s fix that!

The red line under the code class UsersAdapter.

Click on UsersAdapter, then, on your keyboard, press Alt+Enter on Windows (or Option+Enter on a Mac). Then click implement members. Now, select all three methods, and press OK.

Control click on each of the three methods. (Or Shift click the top, then bottom method.)

Android Studio will now stub out the following methods, and should look like the following.

Our RecyclerView, now with the three methods we generated.

Next, let’s focus on the data that we will insert into this RecyclerView. Let’s have UsersAdapter receive a users variable, of type List<User>. This indicates that we will have a List of User objects. There could be 10 users, 50 users, or 423 users. At this point, we really don’t care. We just know that UsersAdapter will receive a List of Users.

Next, let’s turn our attention to the getItemCount method. This method needs to return the total number of items in this RecyclerView. In the last step, we made UsersAdapter receive a List of Users. Now, we can use the users variable to get the size of the List.

The getItemCount method in our RecyclerView. This returns the total number of items our RecyclerView contains.

Next, let’s work on onCreateViewHolder. This method is responsible for setting the XML layout file we want to use. In the part one, we created row_user.xml. We’ll use that layout here.

First, let’s rename the variables of the method to give them more meaning. p0 and p1 are not great variable names. Let’s change that to parent and viewType, as you can see above. Next, we create a variable named view and use LayoutInflater.from to create — or inflate — the XML layout file. Then we return ViewHolder, passing in our view variable.

Change the return type of onCreateViewHolder to ViewHolder, the class at the bottom of the UsersAdapter.kt file.

The method onCreateViewHolder should return the ViewHolder class at the bottom of the screen.

After setting up onCreateViewHolder, let’s focus on how the RecyclerView receives the List of Users. Open content_main.xml and ensure that the RecyclerView tag has an android:id attribute set to recyclerView.

content_main.xml with the added id tag.

With content_main.xml finished, open MainActivity.kt and delete the boilerplate code to make the file look like the following.

We can now setup the RecyclerView in MainActivity. Using a new Kotlin feature, let’s code the following.

The new recyclerView.apply { } code in onCreate of the MainActivity.kt file.

You’ll notice the red variable users on line 18 above. The only task left to do is to pass the users variable from MainActivity into UsersAdapter. For now, let’s fill the RecyclerView with generated content. To start, we need to create a Model that holds our data.

Right-click on the package name. Then select New and Kotlin File/Class.

Set the class Name of User and Kind of Class. This will automatically generate a User.kt file for us that we can use to store the user’s info.

Let’s convert this class into a data class and add a firstName and lastName, both as a String.

A Kotlin data class that will hold the users’s first and last name.

In part three, we’ll finish coding the RecyclerView!

About the author

Daniel Malone
Bringing more than a decade of software engineering experience, Daniel Malone is Editor at androidEveryday. An Austin native, Daniel is often found reading technical books, blogging and creating YouTube tutorials. When not working, he likes to listen to pop hits on Google Play Music.

Android RecyclerView Tutorial (Part 1)

  • Most apps contain lists of data using RecyclerView.
  • In this tutorial, use ConstraintLayout and Kotlin to display a list of data.

2 months ago

findViewById() in Kotlin

  • As you may have discovered, findViewByid() is no longer needed.
  • Kotlin adds the ability to directly access Views.

2 months ago

Android Architecture Components Tutorial: ViewModel + LiveData

  • Use Kotlin to build a basic Android app using Android Architecture Components.
  • As part of Jetpack, LiveData and ViewModel support a MVVM app architecture.

3 months ago