Android RecyclerView


A RecyclerView is an adapterview that allows us display a large data set through just a limited window.

RecyclerViews were introduced in Android API 5 as an alternative to ListViews. ListView as you may already know is also an adapterview and allows us display items in a vertically scrolling list.

The term RecyclerView comes from a combination of two words: Recycler and View.

The term Recycler comes from the verb recycle which means to treat things that have already been used so that they can be used again if Oxford Advanced Learner's Dictionary* is to be believed.

Collins Cobuild English Dictionary tells us that a view refers to the way in which a piece of text or graphics is displayed on a computer screen.

And to be honest those two terms in a way defines us exactly what a RecyclerView is. In that a RecyclerView recycles views. It reuses views. And those views are just pieces of grahpical user interface widgets that normally we define in XML and get inflated in java.

RecyclerViews are not limited to any one particular view. Instead it can utilize any. Be it TextViews, Buttons, checkBoxes etc.

RecyclerView is meant to work as an adapterview, basically with an adapter that adapts data to the RecyclerView's views. AdapterViews normally display collections of data.

Even though there have been various great and popular adapterviews like ListViews, GridViews and expandablelistviews, recyclerview is the best when it comes to displaying large quantities of data.

And that's because of it's concept of recycling already used views instead of re-inflating them everytime.

Infation of views is normally expensive as it involves parsing of XML layouts into java objects. And remember this is to be done in realtime as the user scrolls through a list of data. Users can definitely notice that. And you can imagine having hundreds of rows or grids of data. Not so good isn't it?

So RecyclerView is great in this regard as instead of re-inflating the views, it holds them in a ViewHolder class and then recycles them, only binding fresh data to them. The end result is smoothness even with large datasets that we and our users can take for granted.

Advantages of RecyclerViews

No. Advantage Description
1. Efficient. Android Engineers introduced RecyclerViews to provide a more efficient way of displaying large data sets. Previously ListViews were and gridviews were the only inbuilt alternatives. They are great for small data sets but not for large.
2. Flexible RecyclerViews are the most flexible adapterviews in android. I have seen people using them to build almost anything from creating input forms to scrollable grids to other adapterviews.
3. Easy to use. This is one of the reasons why they are popular. Some may argue that you always need two or three classes however, these are simple classes that can be combined in one file.
4. Obeys SOC(Seperation of Concerns) A RecyclerView decouples adapter from views. Hence we can work on either without touching the other.

Programmatic Definition of RecyclerViews

Like anything you can imagine in android or even java at large, a RecyclerView is just a class:

class RecyclerView{..} 

This clas is defined in a package:

package android.support.v7.widget;

And is public so that it's not only visible to class in this package but also those in other packages:

public class RecyclerView{..} 

RecyclerView extends android.view.ViewGroup. ViewGroup is a special view that can contain other views. It can basically hold children within itself. Think of views like relativelayout and linearlayout.

public class RecyclerView extends ViewGroup... {..}

This will give a RecyclerView the capability to hold views as well.

Then RecyclerView implements two interfaces:

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild{..}
  1. ScrollingView - An interface housed in the android.support.v4.view package. It provides scroll related APIs to RecyclerView. These includes APIs such as computeHorizontalScrollExtent(),computeVerticalScrollExtent() etc.

  2. NestedScrollingChild - Also belongs to android.support.v4.view and provides support for dispatching of nested scrolling operations to a cooperating parent ViewGroup.

20+ RecyclerView Examples

Let's look at some examples.

Android RecyclerView Examples.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post