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?
Advantages of RecyclerView over listview : Contains ViewHolder by default. Easy animations. Supports horizontal , grid and staggered layouts.
RecyclerView has greater support for LayoutManagement including vertical lists, horizontal lists, grids and staggered grids. ListView only supports vertical lists. ListView starts by default with dividers between items and requires customisation to add decorations.
Different from the ListView, the RecyclerView does not have the responsibility to position the elements of the list, and, besides what its name may imply, it does not have the function to recycle the views either. All of those functionalities are provided by the LayoutManagers.
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they're needed. As the name implies, RecyclerView recycles those individual elements.
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.
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.
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.
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.
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.
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:
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.
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With