As per the official documentation RecyclerView
is a major enhancement over ListView
. It contains many new features like ViewHolder
, ItemDecorator
, LayoutManager
, and SmoothScroller
. But one thing that certainly gives it an edge over the ListView
is; the ability to have animations while adding or removing an item.
View Holders
In ListView
, defining view holders was a suggested approach for
keeping references for views. But it was not a compulsion. Although by
not doing so, ListView
used show stale data. Another major drawback of
not using view holders could lead to a heavy operation of finding
views by ids every time. Which resulted in laggy ListView
s.
This problem is solved in RecylerView
by the use of
RecyclerView.ViewHolder class. This is one of the major differences in
RecyclerView
and ListView
. When implementing a RecyclerView
this class
is used to define a ViewHolder
object which is used by the adapter to
bind ViewHolder
with a position. Another point to be noted here, is
that while implementing the adapter for RecyclerView
, providing a
ViewHolder
is compulsory. This makes the implementation a little
complex, but solves the issues faced in ListView
.
Layout Manager
When speaking of ListView
s, only one type of ListView
is available
i.e. the vertical ListView
. You cannot implement a ListView
with
horizontal scroll. I know there are ways to implement a horizontal
scroll, but believe me it was not designed to work that way.
But now when we look at Android RecyclerView
vs ListView
, we have
support for horizontal collections as well. In-fact it supports
multiple types of lists. To support multiple types of lists it uses
RecyclerView.LayoutManager
class. This is something new that ListView
does not have. RecyclerView
supports three types of predefined Layout
Managers:
LinearLayoutManager – This is the most commonly used layout manager in
case of RecyclerView. Through this, we can create both horizontal and
vertical scroll lists.
StaggeredGridLayoutManager – Through this
layout manager, we can create staggered lists. Just like the Pinterest
screen.
GridLayoutManager– This layout manager can be used to display
grids, like any picture gallery.
Item Animator
Animations in a list is a whole new dimension, which has endless
possibilities. In a ListView, as such there are no special provisions
through which one can animate, addition or deletion of items. Instead
later on as android evolved ViewPropertyAnimator was suggested by
Google’s Chet Haase in this video tutorial for animations in ListView
.
On the other hand comparing Android RecyclerView
vs ListView
, it has
RecyclerView.ItemAnimator class for handling animations. Through this
class custom animations can be defined for item addition, deletion and
move events. Also it provides a DefaultItemAnimator, in case you don’t
need any customizations.
Adapter
ListView
adapters were simple to implement. They had a main method
getView
where all the magic used to happen. Where the views were bound
to a position. Also they used to have an interesting method
registerDataSetObserver where one can set an observer right in the
adapter. This feature is also present in RecyclerView, but
RecyclerView.AdapterDataObserver class is used for it. But the point
in favor of ListView is that it supports three default implementations
of adapters:
ArrayAdapter
CursorAdapter
SimpleCursorAdapter
Whereas RecyclerView
adapter, has all the functionality that ListView
adapters had except
the built in support for DB cursors and ArrayLists. In
RecyclerView.Adapter
as of now we have to make a custom implementation
to supply data to the adapter. Just like a BaseAdapter does for
ListView
s. Although if you wish to know more about RecyclerView
adapter implementation, please refer to Android RecyclerView Example.
Item Decoration
To display custom dividers in a ListView, one could have easily added
these parameters in the ListView XML:
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
The interesting part about Android
RecyclerView is that, as of now it does not show a divider between
items by default. Although the guys at Google must have left this out
for customization, intentionally. But this greatly increases the
effort for a developer. If you wish to add a divider between items,
you may need to do a custom implementation by using
RecyclerView.ItemDecoration class. Or you can apply a hack by using
this file from official samples: DividerItemDecoration.java