Android Recyclerview vs ListView with Viewholder
Asked Answered
E

7

156

I recently came across the android RecyclerView which was released with Android 5.0 and it seems that RecyclerView is just an encapsulated traditional ListView with the ViewHolder pattern incorporated into it, which promotes the reuse of the view, rather than creating it every single time.

What are the other benefits of using RecyclerView ? If both have the same effect in terms of performance, why would one prefer RecyclerView` ?

Edit

I found that people have asked similar question and the answers are not conclusive, adding them here for record keeping.

Recyclerview vs Listview

Should we use RecyclerView to replace ListView?

Why doesn't RecyclerView have onItemClickListener()? and How RecyclerView is different from Listview?

Evolute answered 15/2, 2015 at 10:24 Comment(5)
Because the RecyclerView is much faster and more versatile with a much better API. Things like animating the addition or removal of items are already implemented in the RecyclerView without you having to do anything. There is no question about it, throw your ListView in the trash can, the RecyclerView is here to steal the show.Jeunesse
You can associate a layout manager with a RecyclerView, so they're not limited to vertically scrolling lists. This is quite powerful additional functionality.Kist
@Kist - What do you mean by "not limited to vertically scrolling lists" ? Are you saying that the Recycle View can act as an "placeholder" for Gridviews and ListViews too ?Evolute
@XaverKapeller - It would be great if you could list the differences between the two and answer the question rather than on a comment, so that it might help me and the others in the future who may be wondering about the same thing ?Evolute
@Kist - Could you provide a bit detail about what you meant and answer the question rather than on a comment. Thanks for taking the timeEvolute
C
12

The other plus of using RecycleView is animation, it can be done in two lines of code

RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
        recyclerView.setItemAnimator(itemAnimator);

But the widget is still raw, e.g you can't create header and footer.

Cepheus answered 15/2, 2015 at 10:39 Comment(2)
And you never will be able to create header and footer in that sense. They're just other view types in your adapter. List view wraps your adapter in HeaderViewListAdapter and adds header support in the background. With RecyclerView you're the one in control.Wingover
RecyclerView uses a DefaultItemAnimator by default. Then why did you use this code?Larder
E
9

Okay so little bit of digging and I found these gems from Bill Philips article on RecycleView

RecyclerView can do more than ListView, but the RecyclerView class itself has fewer responsibilities than ListView. Out of the box, RecyclerView does not:

  • Position items on the screen
  • Animate views
  • Handle any touch events apart from scrolling

All of this stuff was baked in to ListView, but RecyclerView uses collaborator classes to do these jobs instead.

The ViewHolders you create are beefier, too. They subclass RecyclerView.ViewHolder, which has a bunch of methods RecyclerView uses. ViewHolders know which position they are currently bound to, as well as which item ids (if you have those). In the process, ViewHolder has been knighted. It used to be ListView’s job to hold on to the whole item view, and ViewHolder only held on to little pieces of it.

Now, ViewHolder holds on to all of it in the ViewHolder.itemView field, which is assigned in ViewHolder’s constructor for you.

Evolute answered 15/2, 2015 at 15:15 Comment(0)
C
4

More from Bill Phillip's article (go read it!) but i thought it was important to point out the following.

In ListView, there was some ambiguity about how to handle click events: Should the individual views handle those events, or should the ListView handle them through OnItemClickListener? In RecyclerView, though, the ViewHolder is in a clear position to act as a row-level controller object that handles those kinds of details.

We saw earlier that LayoutManager handled positioning views, and ItemAnimator handled animating them. ViewHolder is the last piece: it’s responsible for handling any events that occur on a specific item that RecyclerView displays.

Choriamb answered 19/2, 2015 at 15:40 Comment(0)
C
2

I used a ListView with Glide image loader, having memory growth. Then I replaced the ListView with a RecyclerView. It is not only more difficult in coding, but also leads to a more memory usage than a ListView. At least, in my project.

In another activity I used a complex list with EditText's. In some of them an input method may vary, also a TextWatcher can be applied. If I used a ViewHolder, how could I replace a TextWatcher during scrolling? So, I used a ListView without a ViewHolder, and it works.

Chou answered 27/9, 2016 at 10:2 Comment(2)
I used a ListView without a ViewHolder, and it works. terrible idea ... how could I replace a TextWatcher during scrolling? there is no need to replace it ... just TextWacher should put data into different container after reusing ... and it can be done really easyLindell
@Selvin, thanks for your opinion. Now I cannot edit that project. There were several TextWatchers on a screen. Probably you are right, but I cannot check it out.Chou
B
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.

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.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//or
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
  • 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.

LayoutManager

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.

Bryna answered 23/11, 2018 at 13:11 Comment(0)
A
1

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.

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 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.

Aara answered 8/6, 2019 at 10:25 Comment(0)
S
0

If you use RecycleView, first you need more efford to setup. You need to give more time to setup simple Item onclick, border, touch event and other simple thing. But end product will be perfect.

So decision is yours. I suggest, if you design simple app like phonebook loading, where simple click of item is enough, you can implement listview. But if you design like social media home page with unlimited scrolling. Several different decoration between item, much control of individual item than use recycle view.

Spacial answered 15/3, 2020 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.