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.
The picture above can be easily created by ListView
using custom adapter.
So, in what situation should one use RecyclerView
?
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
.
Example:
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//or
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
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.
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.
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.
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