Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Aniket Kadam
Aniket Kadam

Posted on

     

Doing Android Long Lists Effectively

You're going to need a RecyclerView no doubt, but making the right choice of adapter can reduce your work tremendously and set you up for success.

If you aren't ever going to change the items in a list while the user's looking at them, this article won't matter to you. But if you are, this will be the simplest, nicest animating, easiest to reason about implementation of it you'd have ever seen.

Normally you'd choose aRecyclerview.ViewHolder but in almost all cases involving changing lists, you're better off choosing theListAdapter

The key difference between the two, is that the ListAdapter includes an easy way to notify which items have changed by giving us a nearly complete implementation of theDiffUtil.ItemCallback.

Normally, implementing this involves a bit of boilerplate but the ListAdapter abstracts all that out for you.

This handles all the complicated diffing code and you can just focus on providing up-to-date lists of data via sending them to the ListAdapter method

publicvoidsubmitList(@NullableList<T>list)

So now all you've got to implement are:

onCreateViewHolder,onBindViewHolder, and the DIFF_CALLBACK.

Then when you have a different list, send it to theadapter.submitList(list).

The first two are standard functions, so their implementation is left as an exercise to the reader.

However, we'll be focusing on the DIFF_CALLBACK. Here's the relevant section:

ListAdapter takes ones of these as a constructor parameter.

Creating a static version of it is great because the interface is built to abstract over comparing two instances at a time, and these are always passed in.

The implementation has two functions, one meant to be a lightweight comparisonareItemsTheSame this should be an id or a field that will be compared.
areContentsTheSame does a deep comparison and checks if the objects are exactly the same.
This speeds up comparisons between objects because if they're not the same, a deep comparison doesn't have to be done at all.

I wish theofficial docs made it a bit clearer!

Here's what that looks like:

classAstronomyPicListAdapter:ListAdapter<AstronomyPic,ApodDetailViewHolder>(DIFF_CALLBACK){companionobject{// We create a static version since this doesn't keep state, it only compares the instances it's passed as function parameters.valDIFF_CALLBACK=object:DiffUtil.ItemCallback<AstronomyPic>(){overridefunareItemsTheSame(oldItem:AstronomyPic,newItem:AstronomyPic):Boolean=oldItem.date==newItem.date// Usually a deep comparison.overridefunareContentsTheSame(oldItem:AstronomyPic,newItem:AstronomyPic):Boolean=oldItem==newItem}}// Standard oncreateViewHolder using DatabindingoverridefunonCreateViewHolder(parent:ViewGroup,viewType:Int):ApodDetailViewHolder{returnApodDetailViewHolder(DataBindingUtil.inflate<DetailListApodItemBinding>(LayoutInflater.from(parent.context),R.layout.detail_list_apod_item,parent,false))}// Standard bindViewHolder that uses a bind method and only passes in the data if it was non-nulloverridefunonBindViewHolder(holder:ApodDetailViewHolder,position:Int){getItem(position)?.let{holder.bind(it)}}}

What this does for you:

  1. You now only have to get a new list and 'submit' it to the adapter.
  2. The adapter calculates what values have actually changed.
  3. And calls notifyItemChanged on only those specific values.

This results in:

  1. Better animations
  2. Less work for you
  3. Never having to think about notifyItemChanged or dread telling the recyclerview that all times have changed!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
vcharantimath profile image
Veeresh Charantimath
  • Location
    Bengaluru, Karnataka, India
  • Work
    Android Dev
  • Joined
  1. areContentsTheSame - is only called if areItemsTheSame returns true

  2. A caveat with ListAdapter - is that it always need to send a new List to perform the diff. Sending the same list will do nothing
    Link -stackoverflow.com/questions/497263...

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

An android dev with ~8 years of experience. I consult and may join fulltime for the right company.
  • Location
    India
  • Work
    Android Developer
  • Joined

More fromAniket Kadam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp