316

From android developer (Creating Lists and Cards):

The RecyclerView widget is a more advanced and flexible version of ListView.

Okay, it sounds cool, but when I saw this example picture, I got really confused about the difference between these two.

enter image description here

The picture above can be easily created by ListView using custom adapter.

So, in what situation should one use RecyclerView?

Surbhi Aggarwal
  • 601
  • 6
  • 20
Blaze Tama
  • 10,129
  • 11
  • 62
  • 119
  • 4
    [first-glance-androids-recyclerview](http://www.grokkingandroid.com/first-glance-androids-recyclerview/) – M D Nov 04 '14 at 06:02
  • 1
    @Dev786: I suggest that you add a comment here explaining in detail what you feel is missing from the **many** existing answers on this question. – CommonsWare Nov 13 '17 at 11:45
  • 1
    [8 Differences between RecyclerView and ListView](https://androidride.com/recyclerview-android-simple-tutorial-listview-checkbox-example/) – Athira Reddy Dec 22 '19 at 07:30

16 Answers16

401

RecyclerView was created as a ListView improvement, so yes, you can create an attached list with ListView control, but using RecyclerView is easier as it:

  1. Reuses cells while scrolling up/down - this is possible with implementing View Holder in the ListView adapter, but it was an optional thing, while in the RecycleView it's the default way of writing adapter.

  2. Decouples list from its container - so you can put list items easily at run time in the different containers (linearLayout, gridLayout) with setting LayoutManager.

Example:

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//or
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
  1. Animates common list actions - Animations are decoupled and delegated to ItemAnimator.

There is more about RecyclerView, but I think these points are the main ones.

So, to conclude, RecyclerView is a more flexible control for handling "list data" that follows patterns of delegation of concerns and leaves for itself only one task - recycling items.

Lii
  • 9,906
  • 6
  • 53
  • 73
daneejela
  • 8,695
  • 6
  • 29
  • 40
  • 19
    Main responsibility of the list view is to 1.) visually arrange items within given area and preferably 2.) reuse items. With RecylerView this breaks into single responsibilities - recycle view recycles and it's given LayoutManager arranges items on the screen. In other words recycle view doesn't know/care where to put items on screen, it just takes care of recycling them. From android doc: "By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. " – daneejela May 21 '15 at 10:11
  • 33
    "Reuse cells while scrolling up/down" : As far I know this is completely wrong Because this happens even in listview without viewholder. – Ruban May 11 '16 at 14:22
  • 17
    The main use of view holder in listview is Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern. A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. – Ruban May 11 '16 at 14:22
  • 7
    What I try to say is Row will be recycled even when you implement listview without view holder. That is the property and advantage of listview. That is what they created listview. – Ruban May 11 '16 at 14:24
  • they did nothing special, these features can be added by anyone. you can extend ListView and baseAdapter and add any feature to it if you want. there actually is no difference between these two. – M D P Sep 06 '16 at 12:41
45

For list views to have good performance you'll need to implement the holder pattern, and that's easy to mess up especially when you want to populate the list with several different kinds of views.

The RecyclerView bakes this pattern in, making it more difficult to mess up. It's also more flexible, making it easier to handle different layouts, that aren't straight linear, like a grid.

CaptRespect
  • 1,694
  • 1
  • 13
  • 22
  • 6
    Yes ! , there should be more discussions on when *NOT* to use recyclerview too. If your rows have content dynamically added based on the model's data and you use Recycler view, BOOM. – Rana Deep Aug 19 '15 at 12:27
  • 1
    You should be able to add dynamic content based on model data. You'll just need to setup different views for any type of content you want to support. – CaptRespect Aug 31 '15 at 14:43
42

ListView is the ancestor to RecyclerView. There were many things that ListView either didn't do, or didn't do well. If you were to gather the shortcomings of the ListView and solved the problem by abstracting the problems into different domains you'd end up with something like the recycler view. Here are the main problem points with ListViews:

  • Didn't enforce View Reuse for same item types (look at one of the adapters that are used in a ListView, if you study the getView method you will see that nothing prevents a programmer from creating a new view for every row even if one is passed in via the convertView variable)

  • Didn't prevent costly findViewById uses(Even if you were recycling views as noted above it was possible for devs to be calling findViewById to update the displayed contents of child views. The main purpose of the ViewHolder pattern in ListViews was to cache the findViewById calls. However this was only available if you knew about it as it wasn't part of the platform at all)

  • Only supported Vertical Scrolling with Row displayed Views (Recycler view doesn't care about where views are placed and how they are moved, it's abstracted into a LayoutManager. A Recycler can therefore support the traditional ListView as shown above, as well as things like the GridView, but it isn't limited to that, it can do more, but you have to do the programming foot work to make it happen).

  • Animations to added/removed was not a use case that was considered. It was completely up to you to figure out how go about this (compare the RecyclerView. Adapter classes notify* method offerings v. ListViews to get an idea).

In short RecyclerView is a more flexible take on the ListView, albeit more coding may need to be done on your part.

Jim Baca
  • 5,684
  • 2
  • 20
  • 30
20

The RecyclerView is a new ViewGroup that is prepared to render any adapter-based view in a similar way. It is supossed to be the successor of ListView and GridView, and it can be found in the latest support-v7 version. The RecyclerView has been developed with extensibility in mind, so it is possible to create any kind of layout you can think of, but not without a little pain-in-the-ass dose.

Answer taken from Antonio leiva

 compile 'com.android.support:recyclerview-v7:27.0.0'

RecyclerView is indeed a powerful view than ListView . For more details you can visit This page.

IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
  • 6
    I like this sentence from your answer: **This is Android, so things are never easy** This is true for what the Android app development has been being, but I think it's just something wrong in how they designed the API as well as the patterns to build Android app. Theoretically a good design should hide all possible complexities from the programmers (but still be accessisble for advanced ones), what they commonly care is the business logic, data and other algorithms other than struggling with handling UI related problems (and more tedious problems currently present in Android development). – Hopeless Jul 25 '16 at 02:26
18

Following are few key points/differences between RecyclerView & ListView. Take your call wisely.

If ListView works for you, there is no reason to migrate. If you are writing a new UI, you might be better off with RecyclerView.

RecylerView has inbuilt ViewHolder, doesn't need to implement our own like in listView. It support notify at particular index as well

Things like animating the addition or removal of items are already implemented in the RecyclerView without you having to do anything

We can associate a layout manager with a RecyclerView, this can be used for getting random views in recycleview while this was limitation in ListView In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView. Now using a RecyclerView, we can have a

i) LinearLayoutManager - which supports both vertical and horizontal lists, ii) StaggeredLayoutManager - which supports Pinterest like staggered lists, iii) GridLayoutManager - which supports displaying grids as seen in Gallery apps.

And the best thing is that we can do all these dynamically as we want.

Pawan Maheshwari
  • 14,070
  • 1
  • 46
  • 48
15

Major advantage :

ViewHolder is not available by default in ListView. We will be creating explicitly inside the getView(). RecyclerView has inbuilt Viewholder.

Tom11
  • 2,199
  • 6
  • 26
  • 50
Mohammed Imran N
  • 1,248
  • 1
  • 11
  • 25
7

Advantages of RecyclerView over listview :

  1. Contains ViewHolder by default.

  2. Easy animations.

  3. Supports horizontal , grid and staggered layouts

Advantages of listView over recyclerView :

  1. Easy to add divider.

  2. Can use inbuilt arrayAdapter for simple plain lists

  3. Supports Header and footer .

  4. Supports OnItemClickListner .

Manohar Reddy
  • 15,894
  • 7
  • 77
  • 109
  • 5
    As far as I understand you can easily add a divider to a recyclerView using `recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));` after doing something like `recyclerView = view.findViewById(R.id.feed);` – nviens Nov 15 '18 at 16:43
  • 1
    @nviens For list view you can add divider,adjust its height and change its color all in xml it self . Also current implemention for divider is just ok , it was a nightmare to add divider for recyclerview previously , just check this answer to see how previous implementation for divider was https://stackoverflow.com/a/27037230/6478047 – Manohar Reddy Nov 16 '18 at 04:14
5

I think the main and biggest difference they have is that ListView looks for the position of the item while creating or putting it, on the other hand RecyclerView looks for the type of the item. if there is another item created with the same type RecyclerView does not create it again. It asks first adapter and then asks to recycledpool, if recycled pool says "yeah I've created a type similar to it", then RecyclerView doesn't try to create same type. ListView doesn't have a this kind of pooling mechanism.

Tom11
  • 2,199
  • 6
  • 26
  • 50
Mustafa Güven
  • 14,422
  • 10
  • 56
  • 80
4

In addition to above differences following are few more:

  1. RV separates view creation and binding of data to view. In LV, you need to check if convertView is null or not for creating view, before binding data to it. So, in case of RV, view will be created only when it is needed but in case of LV, one can miss the check for convertview and will create view everytime.

  2. Switching between Grid and List is more easy now with LayoutManager.

  3. No need to notify and update all items, even if only single item is changed.

  4. One had to implement view caching in case of LV. It is provided in RV by default. (There is difference between view caching n recycling.)

  5. Very easy item animations in case of RV.

Virat18
  • 2,797
  • 1
  • 21
  • 28
2

In my opinion RecyclerView was made to address the problem with the recycle pattern used in listviews because it was making developer's life more difficult. All the other you could handle more or less. For instance I use the same adapter for ListView and GridView it doesn't matter in both views the getView, getItemCount, getTypeCount is used so it's the same. RecyclerView isn't needed if ListView with ListAdapter or GridView with grid adapters is already working for you. If you have implemented correctly the ViewHolder pattern in your listviews then you won't see any big improvement over RecycleView.

Tom11
  • 2,199
  • 6
  • 26
  • 50
aimiliano
  • 970
  • 2
  • 11
  • 18
2

I worked a little with RecyclerView and still prefer ListView.

  1. Sure, both of them use ViewHolders, so this is not an advantage.

  2. A RecyclerView is more difficult in coding.

  3. A RecyclerView doesn't contain a header and footer, so it's a minus.

  4. A ListView doesn't require to make a ViewHolder. In cases where you want to have a list with sections or subheaders it would be a good idea to make independent items (without a ViewHolder), it's easier and doesn't require separate classes.

CoolMind
  • 16,738
  • 10
  • 131
  • 165
  • 1
    you can make header and content side in recyclerview adapter and it will be more flexible than listview. just hard to change ur mind listview to recyclerview but if you can do this,you will understand what i mean.listview deprecated just go try to learn recyclerview goodluck – erginduran Sep 25 '16 at 12:55
  • 2
    @erginduran, I used both. ListView is still widely used and is not deprecated. Also it wastes less memory than RecyclerView. If you want only to replace a ListView with RecyclerView, it won't be a good idea. – CoolMind Sep 26 '16 at 06:15
  • just check why this guys developed recyclerview for? its improvement of listview, right? there are many old libs and apps in google play so you are right listview is still widely used.just leave old things in the past. check comparisons -> [link](http://stackoverflow.com/questions/28525112/android-recyclerview-vs-listview-with-viewholder) – erginduran Sep 26 '16 at 18:11
  • @erginduran, thanks. You are right, RecyclerView has some advantages. My answer was about usual tasks in where a ListView is sometimes more easier than a RecyclerView. Of course, animation and some other improvements are difficult or not possible in a ListView. – CoolMind Sep 26 '16 at 18:39
  • I added some aspects in that topic, see http://stackoverflow.com/a/39721769/2914140. – CoolMind Sep 27 '16 at 10:03
2

RecyclerView info

The RecyclerView was introduced with Android 5.0 (Lollipop). it is included in the Support Library. Thus, it is compatible with Android API Level 7.

Similarly to the ListView, RecyclerView’s main idea is to provide listing functionality in a performance friendly manner. The ‘Recycler’ part of this view’s name is not there by coincidence. The RecyclerView can actually recycle the items with which it’s currently working. The recycling process is done thanks to a pattern called View Holder.

Pros & Cons of RecyclerView

Pros:

  • integrated animations for adding, updating and removing items
  • enforces the recycling of views by using the ViewHolder pattern
  • supports both grids and lists
  • supports vertical and horizontal scrolling
  • can be used together with DiffUtil

Cons:

  • adds complexity
  • no OnItemClickListener

ListView info

The ListView has been around since the very beginning of Android. It was available even in API Level 1 and it has the same purpose as the RecyclerView.

The usage of the ListView is actually really simple. In this aspect, it’s not like its successor. The learning curve is smoother than the one for the RecyclerView. Thus, it is easier to grasp. We don’t have to deal with things like the LayoutManager, ItemAnimator or DiffUtil.

Pros & Cons of ListView

Pros:

  • simple usage
  • default adapters
  • available OnItemClickListener
  • it’s the foundation of the ExpandableListView

Cons:

  • doesn’t embrace the usage of the ViewHolder pattern
Basi
  • 2,675
  • 19
  • 28
1
  1. You can use an interface to provide a click listener. I use this technique with ListViews, too.
  2. No divider: Simply add in your row a View with a width of match_parent and a height of 1dp and give it a background color.
  3. Simply use a StateList selector for the row background.
  4. addHeaderView can be avoided in ListViews, too: simply put the Header outside the View.

So, if efficiency is your concern, then yes, it's a good idea to replace a ListView with a RecyclerView.

Jay Patel
  • 1,011
  • 8
  • 24
1

There are many differences between ListView and RecyclerView, but you should be aware of the following in particular:

  • The ViewHolder pattern is entirely optional in ListView, but it’s baked into RecyclerView.
  • ListView only supports vertical scrolling, but RecyclerView isn’t limited to vertically scrolling lists.
sachin pareek
  • 373
  • 6
  • 18
0

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

You find it harder because you are thinking just with the Android library in mind.

Today there exists a lot of options that help you build your own adapters, making it easy to build lists and grids of dynamic items that you can pick, reorder, use animation, dividers, add footers, headers, etc, etc.

Don't get scared and give a try to RecyclerView, you can starting to love it making a list of 100 items downloaded from the web (like facebook news) in a ListView and a RecyclerView, you will see the difference in the UX (user experience) when you try to scroll, probably the test app will stop before you can even do it.

I recommend you to check this two libraries for making easy adapters:

FastAdapter by mikepenz

FlexibleAdapter by davideas

Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
Erick Moya
  • 11
  • 1
  • 1
  • 1
    I think your statements about the performance difference between the two are exaggerated. I've been using ListView in a production app since 2012, displaying up to 3000 items with ~10 different Views in each row, absolutely no problems performance wise even on old Android 2.3 devices. – Magnus W Oct 01 '18 at 08:13
0

I want just emphasize that RecyclerView is a part of the compatibility package. It means that instead of using the feature and code from OS, every application carries own RecyclerView implementation. Potentially, a feature similar to RecyclerView can be a part of a future OS and using it from there can be beneficial. For example Harmony OS will be out soon.The compatibility package license can be changed in the future and it can be an implication. Summery of disadvantages:

  1. licensing
  2. a bigger foot print especially as a part of many apps
  3. losing in efficiency if some feature coming from OS can be there

But on a good note, an implementation of some functionality, as swiping items, is coming from RecyclerView.

All said above has to be taken in a consideration.

user2305886
  • 669
  • 8
  • 16