Android Recyclerview vs ListView with Viewholder
Solution 1
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.
Solution 2
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 methodsRecyclerView
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, andViewHolder
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.
Solution 3
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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Mushtaq Jameel
"That's how you devour a whale, Doug. One bite at a time." - Frank Underwood, House of Cards. I strongly believe that if one is focused on that one bite rather than the whale, we would eventually eat the whole whale, the same principle applies to everything in life, with enough perseverance and discipline almost all things can be achieved.
Updated on July 08, 2022Comments
-
Mushtaq Jameel almost 2 years
I recently came across the android
RecyclerView
which was released with Android 5.0 and it seems thatRecyclerView
is just an encapsulated traditionalListView
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.
-
Xaver Kapeller about 9 yearsBecause 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 theRecyclerView
without you having to do anything. There is no question about it, throw yourListView
in the trash can, theRecyclerView
is here to steal the show. -
Alan about 9 yearsYou can associate a layout manager with a RecyclerView, so they're not limited to vertically scrolling lists. This is quite powerful additional functionality.
-
Mushtaq Jameel about 9 years@Alan - 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 ?
-
Mushtaq Jameel about 9 years@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 ?
-
Mushtaq Jameel about 9 years@Alan - Could you provide a bit detail about what you meant and answer the question rather than on a comment. Thanks for taking the time
-
Athira Reddy over 4 years
-
-
Eugen Pechanec almost 9 yearsAnd 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. WithRecyclerView
you're the one in control. -
Selvin over 6 yearsI 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 easy
-
CoolMind over 6 years@Selvin, thanks for your opinion. Now I cannot edit that project. There were several
TextWatcher
s on a screen. Probably you are right, but I cannot check it out. -
Athira Reddy over 4 yearsRecyclerView uses a DefaultItemAnimator by default. Then why did you use this code?