How Recycler view works in android

Recycle View

I recently did a mini-project Quote app, I used listview for that. The project started consuming lots of memory and CPU whenever I scroll through the quotes. After reading few articles and watching videos. I realized there is a better way to show a list in android, which is Recycler View. In this post, we will discuss why we should avoid using list view then how the Recycler view solves that problem.

Problems with list view:

Consider you want to show a list of quotes, each quote has 2 fields one quotes text and another is the author name. Let’s call this a ViewHolder. What List view does is if you 100 quotes, it will create 100 ViewHolder. This leads to problems like scroll lag, high memory consumption, and etc. This can be optimized because users will never see all 100 quotes in a single view. The ViewHolder should be recycled. My friend, now you know what recycler view gonna do and why we need it.

Recycler View

As discussed, if we have 100 quotes and we are gonna show 7 quotes in a single fold. The recycler view says we create 7 ViewHolder, when a user scrolls down we use 1st ViewHolder and change the data, and set the value of the 8th quote. Here we are recycling the ViewHolder again and again by just changing the value.

In technical terms,

Recycler View

In our example, if a quote goes out of the view we call it a Scrap view, these views are collected and know as recycle views. The ViewHolder which we will be reusing is known as Dirty View.

How it is implemented?

To understand the implementation, we should understand the following terminologies

Recycler View: It is a collection of ViewHolders

Layout Manager: This tells us how the Recycler view should be shown, horizontal scrolling, vertical scrolling, grid, etc.

Adapter: This is the core component in our process, adapter holds the responsibility likes create a new ViewHolder and populate the data from the data store. 

ViewHolder: It holds the view, in our case, it is the quote text and author.

The Process:

RecyclerView and Layout manager are UI parts, now we want to populate the data and add a mechanism to recycle the view. This logic is entirely written in the adapter.

The adapter should have access to the datastore, we extend RecyclerView.Adapter class provided by the Android library. We have to override the following functions.

  1. onCreateViewHolder – This function will return a new ViewHolder, this will be used whenever we wanna create a new ViewHolder, probably for the 1st fold view.
  2. onBindViewHolder – This function is responsible to populate the ViewHolder, this function gets the ViewHolder and the position for which we have to fill the quote as the parameters. For example, if my datastore is a list given the ViewHolder and position I can fill the data.
  3. getItemCount – Total items in the list.

I hope this gave you some clarity. Happy Reading.

Related Post

Leave a Reply

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