- Lucas' Newsletter
- Posts
- Daily Dispatch 22 - ListAdapter to the Rescue
Daily Dispatch 22 - ListAdapter to the Rescue
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/520f3a9e-0e73-4148-9f64-fdc65611f993/pexels-photo-239548.jpeg?t=1722263357)
It’s been a while since I’ve written about some “normal” Android Development.
Today I followed a lead on DiffUtil, a library that a senior developer told me about. It is supposed to be used in Recycler views with adapters to avoid having to call notifyAllDataChanged() and having some uneasy visual bugs.
So I looked into it and found some lovely blog posts on the matter.
One that I found really enlightening was this one.
So I went back to my MovieApp to fix my visual bugs.
This is the old way of using RecyclerView.Adapter:
Now there was a lot going on here. But the worst offender was the updateMoviesList calling notifyDataSetChanged. What this does is forces the UI to re-render the whole view, even on items that haven’t changed from the previous movie list.
So what was the solution? First I implemented this Callback class, that uses DiffUtil. Which is basically a class that facilitates comparison between items. And more importantly, will be used later on the new Adapter.
After this was done, I went to restructure my MovieListAdapter class, so it inherits from ListAdapter, which is a improved version of dealing with lists. Less boilerplate code and integration with DiffUtil.
Here’s the new declaration:
As you can see I took some liberty with MovieViewHolder as well, switched from binding to view. I decided to start using viewBinding instead of databinding to facilitate my life later on.
Here’s the new functioning code for the MovieListAdapter:
There’s only one subtle detail about calling the movieListAdapter to update the list, we need to change any old invocations of adapter.updateMoviesList(newList) to adapter.submitList(newList).
And that is it for today, see you tomorrow (whenever that is 😉 ).