Posted on

Android RecyclerView vs ListView


android recyclerview

Designed by Freepik

Hi, I saw some people still using ListView and figured it would be nice to talk about why you should forget about it and start using RecyclerView.

It’s Old

ListView is pretty old, although easy to use it’s not customizable. You can’t make it display grids or any other crazy stuff, you’d have to use GridLayout which is old as well. Plus good performance is enabled in RecyclerView by default, in ListView not. If you don’t reuse view holders manually, your scrolling will be glitchy. To switch from list to grid in RecyclerView it takes just one line, animations are supported, custom decorations and more. Plus you’re not likely to see ListView anywhere in production apps, so you definitely need to learn how RecyclerView works if you don’t know yet.

RecyclerView.Adapter

You just need to create a class that extends RecyclerView.Adapter. It has three methods to override.

Just focus on those three overriden methods for now. onCreateViewHolder is called to inflate layouts(create rows). Let’s say your phone can fit 5 list rows in portrait mode, RecyclerView will create something like 7-8 rows, onCreateViewHolder will be called that many times. Doesn’t matter if you’ve got a list of 1000 users, it’s still will be just 7-8 view holders. And it will reuse them automatically.

onBindViewHolder is called for each row when you need to set data to display, it has position in the list, using this you get user from the list and setup your views.

RecyclerView.ViewHolder

The last class that you need to create is ViewHolder.

and simple layout for your rows

You user ViewHolder basically for storing reference to your views and set data on them in onBindViewHolder method

Setup RecyclerView

Finally, add this dependency to build.gradle

Add it to your layout

set it up in Java code

you can choose GridLayoutNamager or many more to change the way it looks completely.

Template

If you follow me, you know I love templates. If you though that creating two classes and layout each time is too much work – templates is a great way. So just copy folder from this repo to your android-studio-installation-location/plugins/android/lib/templates/other

android recyclerview

Restart Android Studio and you’ll have this template

android recyclerview

It will create all those files, the only thing will be left for you is to add RecyclerView to your layout and set it up in Java class.

You can check post about how to change all your default Android Studio templates to use Data Binding here as well.

Data Binding

If you want to get to the next level with RecyclerView, here’s a post for you. Reduces all that annoying code in onBindViewHolder method and makes development faster

OnLickListener/Changes

One last thing, is how to handle row clicks and changes in list. You need to set it in ViewHolder constructor

And you can access any variables that you need from your fields like we have with name

When there’s any changes in the list of users you can just call adapter.notifyDataSetChanged() method, without calling it you won’t see any changes. It will recreate all rows, for optimization purposes there’s notifyItemChanged/Inserted/Removed and many more just so that adapter doesn’t do extra work for list items that hasn’t changed.

 

At first it might seem more code, but it’s pretty much intuitive and using a template saves lots of duplicated actions. If you want to check a GitHub repo, you can check this repo, it uses Data Binding with it. Here’s a good answer why RecyclerView is better than ListView as well. Thanks for reading, don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you think they will benefit from it!