The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.

RecyclerView was made official with Android Lollipop (Api 21) and is much more powerful and flexible compared to its predecessor the ListView. The ListView had been the only way to create Lists in Android, but it had a lot of issues including bad scrolling performance and also handling complex and dynamic rows.

It is important to understand what led to the development of the RecyclerView and why it lives in its own separate library which you can import using gradle with the statement below,

compile com.android.support:recyclerview-v7:25.1.0 

The statement above tells the gradle build system to fetch the RecyclerView library which is part of the Android support library set. So why is it in a separate library, the answer is simple, backward compatibility. Android Support libraries are used to work with features that are not available in all versions of Android. The support library takes care of changing the presentation and behaviour of the component based on the host OS version.

Coming back to ListView, if you created a simple list which has just text in it, like the one below,

xlistactivity10.png.pagespeed.ic.hJc-76OIyv

ListView worked just fine and was fairly easy to work with as well. The list above is a simple list of text which you could save in the app or fetch from an API. But our list is not visually appealing, what do you do when you need a more complex list like the one below,

Weather-Select

There’s a lot more happening in this list, the icons on the left are dynamic and different for each row and so it the temperature on the left. This requires you to work with something called the ViewHolder pattern. It was always recommended with ListViews but not a compulsion. As the name suggests, the ViewHolder pattern helped associate date with a row. Based on the data for each row, we could customize the row by say changing the image to one with clouds if the the temperature associated with the row meant it was cloudy. Working with ViewHolders was very complex and painful and also since Android was not customized to take full advantage of the GPU back then, scrolling a list with dynamic data like this meant stutters and lag.

ViewHolder

The RecyclerView makes this mandatory using the RecyclerView.ViewHolder class. This tight integration makes working with complex table structures in a recycler view very easy. Creating a table structure like the one below is so much easier and faster with RecyclerView,

card_travel

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.

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.

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.

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.

Overall the RecyclerView is a welcome addition to Android and makes building complex UI so much easier. In future posts, we will go through a step by step process of getting started with RecyclerViews and building amazing lists in no time.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s