ListAdapter renewed

ListAdapter renewed

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
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.
With this road to App Bundle you've learned to create and use the App Bundle.

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

Reacties

Populaire posts van deze blog

Android-x86 virtual machine configuration tips

Android and SonarQube with code coverage

Setup SonarQube with Docker