RecyclerView vs. ListView
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 theRecycleView
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));
- 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.
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 aListView
, 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 theconvertView
variable)Didn't prevent costly
findViewById
uses(Even if you were recycling views as noted above it was possible for devs to be callingfindViewById
to update the displayed contents of child views. The main purpose of theViewHolder
pattern inListViews
was to cache thefindViewById
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 traditionalListView
as shown above, as well as things like theGridView
, 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.