Home » Android » performance – Android Recyclerview vs ListView with Viewholder

performance – Android Recyclerview vs ListView with Viewholder

Posted by: admin March 10, 2020 Leave a comment

Questions:

I recently came across the android RecycleView which was released with Android 5.0 and it seems that RecycleView is just an encapsulated traditional ListView with the ViewHolder pattern incorporated into it, which promotes the reuse of the view, rather than the creating it every single time.

What are the other benefits of using RecycleView ?
If both have the same effect in terms of performance, why would one choose to use RecycleView` ?

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?

How to&Answers:

With the advent of Android Lollipop, the RecyclerView made its way officially. The RecyclerView is much more powerful, flexible and a major enhancement over ListView. I will try to give you a detailed insight into it.

1) ViewHolder Pattern

In a ListView, it was recommended to use the ViewHolder pattern but it was never a compulsion. In case of RecyclerView, this is mandatory using the RecyclerView.ViewHolder class. This is one of the major differences between the ListView and the RecyclerView.

It makes things a bit more complex in RecyclerView but a lot of problems that we faced in the ListView are solved efficiently.

2) LayoutManager

This is another massive enhancement brought to the RecyclerView. In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView.

Now using a RecyclerView, we can have a

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.

3) Item Animator

ListViews are lacking in support of good animations, but the RecyclerView brings a whole new dimension to it. Using the RecyclerView.ItemAnimator class, animating the views becomes so much easy and intuitive.

4) Item Decoration

In case of ListViews, dynamically decorating items like adding borders or dividers was never easy. But in case of RecyclerView, the RecyclerView.ItemDecorator class gives huge control to the developers but makes things a bit more time consuming and complex.

5) OnItemTouchListener

Intercepting item clicks on a ListView was simple, thanks to its AdapterView.OnItemClickListener interface. But the RecyclerView gives much more power and control to its developers by the RecyclerView.OnItemTouchListener (No longer supported, please refer to AndroidX) but it complicates things a bit for the developer.

In simple words, the RecyclerView is much more customizable than the ListView and gives a lot of control and power to its developers.

Answer:

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.

Answer:

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 methods RecyclerView
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, and ViewHolder 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.

Answer:

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.

Answer:

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.

Answer:

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.

Answer:

1. 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 ListViews.

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.

2. Layout Manager

When speaking of ListViews, 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.

3. 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.

4. 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
ListViews. Although if you wish to know more about RecyclerView
adapter implementation, please refer to Android RecyclerView Example.

5. Notifying Change in Data

When working with a ListView, if the data set is changed you have to
call the notifyDataSetChanged method of the underlying adapter to
refresh data. Or set the setNotifyOnChange method to true incase you
wish to call the notifyDataSetChanged method automatically. But in
both cases the out come is very heavy on the list. Basically it
refreshes the views of list.

But on the contrary in a RecyclerView adapter, if a single item or a
range of items have changed, there are methods to notify the change
accordingly. Those are notifyItemChanged and notifyItemRangeChanged
respectively and many more like:

notifyItemInsterted notifyItemMoved notifyItemRangeInsterted
notifyItemRangeRemoved And of course it has the original method to
refresh the whole list i.e. notifyDataSetChanged which notifies the
adapted the whole data set has changed.

6. Item Decoration

To display custom dividers in a ListView, one could have easily added
these parameters in the ListView XML:

XHTML android:divider=”@android:color/transparent”
android:dividerHeight=”5dp” 1 2
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

7. OnItemTouchListener

Listviews used to have a simple implementation for detection of
clicks, i.e. by the use of AdapterView.OnItemClickListener interface.

But on the other hand RecyclerView.OnItemTouchListener interface is
used to detect touch events in Android RecyclerView. It complicates
the implementation a little, but it gives a greater control to the
developer for intercepting touch events. The official documentation
states, it can be useful for gestural manipulations as it intercepts a
touch event before it is delivered to RecyclerView.

Answer:

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:

  1. 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.

  2. 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));

  1. 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.