android

RecyclerView v/s ListView in android

The RecyclerView widget is a more advanced and flexible version of ListView. In the RecyclerView model, several different components work together to display your data. The overall container for your user interface is a RecyclerView object that you add to your layout. The RecyclerView fills itself with views provided by a layout manager that you provide. You can use one of our standard layout managers (such as LinearLayoutManager or GridLayoutManager), or implement your own.
Why RecyclerView?
RecyclerView is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.
When should you use RecyclerView?
You can use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.The RecyclerView widget is a more advanced and flexible version of ListView.There are lots of newly extended functionalities available in RecyclerView that are not directly supported by ListView as following:
Direct horizontal scrolling is not supported in ListView. Whereas in RecyclerView you can easily implement horizontal and vertical scrolling.
ListView supports only simple linear list view with vertical scrolling. Whereas RecyclerView supports three types of lists using RecyclerView.LayoutManager.
1) StaggeredGridLayoutManager
2) GridLayoutManager,
3) LinearLayoutManager
I will give you a brief introduction to all these 3 lists later.
ListView gives you an option to add divider using dividerHeight parameter whereas RecyclerView enables you to customize divider (spacing) between two elements using RecyclerView.ItemDecoration class.
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 to consume and complex.
5) OnItemTouchListener
Intercepting item clicks on a ListView was simple, thanks to its AdapterView.OnItemClickListenerinterface. But the RecyclerView gives much more power and control to its developers by the RecyclerView.OnItemTouchListener but it complicates things a bit for the developer.

Leave a Reply

Your email address will not be published. Required fields are marked *