How to display list of data into Recyclerview in Android

RecyclerView In Android

Recycling is very important as waste has a huge negative impact on the natural environment. Same as real world in computer programming world recycling a memory is very important. Programmer dynamically allocates memory to program, when requested, and free it up for reuse when it is no longer needed. In a same way Recycling play a huge role in Android world. For Displaying scrollable list of items Google introduced Listview with Android SDK API 1. But with complex list of data Listview not provide good performance as it not reuse cell (Recycle) while scrolling up/down (We can achieve this using Viewholder concept in Listview). Recyclerview is introduced in API 21(Lollipop) to overcome all this problems which we face in Listview. Recyclerview is game changer for android developer. Recyclerview is advance and improved version of Listview. Today we are going to discuss about Recyclerview in Android.

Recyclerview Architecture

List of Components of Recyclerview

1. Viewholder:

RecyclerView.ViewHolder class is used by adapter to bind view with a position. ViewHolder holds the information of the view. ViewHolder is compulsory when we use RecyclerView. For making ViewHolder extend class with RecyclerView.ViewHolder.

2. Layout Manager:

LayoutManager is also compulsory when we are using RecyclerView. It is responsible for displaying user interface while we are using RecyclerView. There are 3 types of Predefined LayoutManager for RecyclerView.

1. LinearLayoutManager:
This is most common LayoutManager used for Displaying list of data using RecyclerView. Using LinearLayoutManager we can display list of data vertically as well as horizontally.

2. GridLayoutManager:Using this layout manager we can display data in grid like any photo gallery. Here it gives us lots of flexibility like we can give different span counts.

3. StaggeredGridLayoutManager:Through this layout manager, we can create staggered lists. Just like the Pinterest screen.

3. ItemDecoration:

As per the official Android Developers Documentation An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter’s data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

4. Item Animator:

Animation has touch new heights in real world as well as in Android also. Now imagine if we could add animation in to scrollable list. In ListView there is no special provision for this but in RecyclerView we have ItemAnimation class for animation on items as changes are made to the adapter. Subclasses of ItemAnimator can be used to implement custom animations for actions on ViewHolder items.

Now Let’s make one android project for better understand how we can display scrollable list of data into RecyclerView.

override fun getItemCount(): Int = listOfMovies.size using this method RecyclerView decide how many rows it will display.

What is BindViewHolder?

For Understanding onBindViewHolder() we need to first check how RecyclerView display data into rows. Let us consider we need to display 100 record in RecyclerView. But RecyclerView will not created all 100 rows at same time. Instead of that RecyclerView creates only the 10 views that are on screen. This way you get 10x better speed and memory usage. But what happens when you start scrolling and need to start showing next views?

Again a simple approach would be to create a new view for each new row that you need to show. But this way by the time you reach the end of the list you will have created 100 views and your memory usage would be the same as in the first approach. And creating views takes time, so your scrolling most probably wouldn’t be smooth.

This is why RecyclerView takes advantage of the fact that as you scroll and new rows come on screen also old rows disappear off screen. Instead of creating new view for each new row, an old view is recycled and reused by binding new data to it. This happens exactly in onBindViewHolder(). Initially you will get new unused view holders and you have to fill them with data you want to display. But as you scroll you’ll start getting view holders that were used for rows that went off screen and you have to replace old data that they held with new data.

Step 5:

Now create one class VerticalSpaceItemDecoration and extend it using RecyclerView.ItemDecoration().

As we can see first assign LinearLayoutManager with RecyclerView.LayoutManager using recyclerViewMovies.layoutManager = LinearLayoutManager(this).Without LayoutManager RecyclerView will not able to display any list of data. Than create object of the adapter using val movieListAdapter = MovieListAdapter(). After attach adapter with RecyclerView using recyclerViewMovies.adapter = movieListAdapter.

Now we have already created two classes for divider and vertical spaces. Use those classes with RecyclerView like this//For Vertical SpacesrecyclerViewMovies.addItemDecoration(VerticalSpaceItemDecoration(48))//For DividersrecyclerViewMovies.addItemDecoration(DividerItemDecoration(this))

Here in this example we have used LinearLayoutManager for displaying list of movies vertically. Now run the project and you will able to see following result.

Summary

From long time we are using ListView. But after MaterialDesign introduced it is hard to manage List of data using ListView. Because using ListView it is hard to manage complex list. With RecyclerView most of the issues related to ListView has been resolved. If you don’t have complex list of data than it is ok to use ListView but if you have complex list of data than it is always recommended to use RecyclerView In Android.

You Can Also Refer

Hi, I am Android Developer and Founder of Android4Dev. Right now my interest is in RxKotlin, MVVM and Dagger2. I am Kotlin Lover. Kotlin made me more productive in terms of coding. Do you want me to code for you ? Please don’t hesitate to contact me.

14 Comments

joker123 download

Hello! Quick question that's totally off topic. Do you know how
to make your site mobile friendly? My website looks weird
when viewing from my iphone4. I'm trying to find a
theme or plugin that might be able to fix this problem.
If you have any recommendations, please share.
Thanks! https://kasino.vin/downloads/67-download-joker123

mobile slot machine download

Tweak it and thereafter place around the website or blog.
It lets your potential customer see a person need to know people are
discussing about. http://www.dealingcity.ru/bitrix/rk.php?goto=http://fanaticoonlinemenu.com/__media__/js/netsoltrademark.php?d=918kiss.bid%2Fdownloads%2F216-download-mega888-for-android-and-ios

about his

Spot on with thօs write-up, I honestly think this sіte needs a
great deal more attention. I'll probably bbe returning to see more, thanks forr the info! https://omovies.org/movie/491707/caffe-italia-montreal-1985-hd-720p-full-movie