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!

Daniel Malone 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.

1 year ago

findViewById() in Kotlin

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

1 year 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.

1 year ago

Android SharedPreferences Tutorial in Kotlin

  • Use SharedPreferences to store simple data in Android Studio projects.
  • It's good for storing small amounts of data

    1 year ago

Cleaner Architecture for Android Apps

  • Clean Architecture for Android can help developers prepare for change.
  • But app developers can't possibly know what will change.

    1 year ago

Android RecyclerView with Kotlin – Part 3

  • In part three of this series, finish creating the RecyclerView.
  • Use onBindViewHolder to display data.

1 year ago

Android and RxJava: Using a Single

  • Use RxJava's Single class when the expected type is either a success or error.
  • It's only slightly different from Observable.just(), which we discussed in the last article.

1 year ago

Android and RxJava: Getting Started

  • The powerful library RxJava can help developers build better apps.
  • See how to use Observables and Observers in this tutorial.

1 year ago

Android RecyclerView with Kotlin - Part 1

  • Oftentimes, developers need to display a list of some sort of data.
  • Whatever type of data is displayed, you should use a RecyclerView.

1 year ago

Display Sample Data when Creating a RecyclerView

  • Android Studio's default RecyclerView preview lacks sample data.
  • Adding sample data can be helpful for debugging.

1 year ago

Android Studio Tutorial with Kotlin (2018 Edition) - Part 2

  • Create a UI in Android Studio using the Design mode.
  • In this latest installment for beginners, we'll add a widget to our screen.

1 year ago