ListAdapter renewed

Android Blogger
3 min readMar 22, 2019

Android RecyclerView with ListAdapter

Every Android developer has worked with the RecyclerView when a list of content needs to be shown. Before the RecyclerView Android offered the widget ListAdapter, with bad performance and several other issues.

Since the Android support lib version 27.1.0 Google added a new ListAdapter which extends RecyclerView.Adapter to make life even easier. Yes it has the name ListAdapter again :(, but for the rest it is completely different!

The simplified ListAdapter

To create a custom ListAdapter override the methods onCreateViewHolder() and onBindViewHolder(). All other methods are already handled by the ListAdapter and don’t need any attention anymore.

  • onCreateViewHolder():

Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.

  • onBindViewHolder():

Called by RecyclerView to display the data at the specified position.

The ListAdapter constructor requests a DiffUtil.ItemCallback, keep reading to found out what it does.

How about DiffUtil.ItemCallback?

When working with a RecyclerView the adapter is responsible to validate if items where added/changed/removed. Previously all actions needed to be controlled manual, these days are over. This task has been taken over by the AsyncListDiffer, a helper for computing differences between two lists with on DiffUtil. Another advantage is that the AsyncListDiffer runs by default on a background thread.

Simply implement a DiffUtil.ItemCallback and provide it as parameter with the constructor as in below example.

The DiffUtil.ItemCallback contains two important functions which need to be implemented:

  • areItemsTheSame():

Called to check whether two items have the same data.

  • areContentsTheSame():

Called to check whether two objects represent the same item.

Let’s go to the total image

You have learned about the ListAdapter and DiffUtil.ItemCallback. Now it’s time to use them to populate a RecyclerView. This example populates a list of cars. Every two seconds one list item is modified to show the animations.

  • activity_main.xml: Main view used within MainActivity.java
  • car_list_row.xml: RecyclerView item used within CarAdapter.java
  • Car.java: The Car object
  • CarAdapter.java: CarAdapter extends the ListAdapter
  • Download.javaData provider, this can Room, Http request or another dataprovider
  • MainActivity.javaThe MainActivity connects all items together and shows all cars
Animated ListAdapter

When you want to build the project locally, download both layout files and copy the java classes from below. With all parts in place the app can run and will show the RecyclerView using the ListAdapter.

Good to know is that the ListAdapter keeps a reference towards the provided list in submitList(). Always make sure to provide an other list with other item instances. If the references to the items are the same, DiffUtil compares the same items which are always equal, and the RecyclerView will not be updated.

Within the MainAcitivty RxJava is used to re-populate the list with modifications. With the ListAdapter Google created another simplified tool for developers to provide the users the best Android experience with the least of costs.

Thank you for reading this article, If you liked the article, help others find this article by sharing it.

Originally published at androidadepth.blogspot.com.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Android Blogger
Android Blogger

Written by Android Blogger

An Android blogger who likes to share knowledge, ideas and solutions :)

No responses yet

Write a response