Press ESC to close

Android RecyclerView

This is an android recyclerview tutorial. We see what a recyclerview is, why we need it and several examples of how to apply it.

What is a 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:

ScrollingView – An interface housed in the android.support.v4.viewpackage. It provides scroll related APIs to RecyclerView. These includes APIs such as computeHorizontalScrollExtent(),computeVerticalScrollExtent() etc.

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

1 : Android RecyclerView – Fill With Array

[center] Android RecyclerView Tutorial – How to fill a recyclerview from an array. [/center]

This is a simple RecyclerView tutorial.Here is what we do:

Fill RecyclerView with data from a simple Array.

We derive from RecycelView.ViewHolder to get our custom viewholder class.

We aslo derive from RecyclerView.Adapter to get our adapter class that helps us bind our data to our RecyclerView.

We create a model layout using cardview that’s inflated to a view item for our recyclerview.

Download

Share Article:

Tags:

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com .
Follow me below;
Github
, and on my channel:
ProgrammingWizards TV