Android Recycler View

In this tutorial you will learn about the Android Recycler View and its application with practical example.

Android Recycler View

Android RecyclerView is more advanced version of ListView and GridView with improved performance and more benefits. It extend ViewGroup and implement ScrollingView and NestedScrollingChlid2 Class. It is very flexible view which provide a limited window for large dataset.
For the use of RecylerView you need to create an Adapter which will extend the RecyclerView.Adapter class.

Table Of Contents

There are three built in Layout Manager which are provided by RecyclerView.

  1. LinearLayoutManager : It shows items in vertical or horizontal scrolling List.
  2. GridLayoutManager : It shows items in Grid.
  3. StaggeredGridLayoutManager : It shows items in a staggered grid.

Lets understand by an example of RecyclerView. Before Start coding we need to add dependencies for RecyclerView in build.gradle file like below Image:

1

Now we have to create Layout for RecyclerView , Check below code:

Attributes :

ID: It is used to identify view uniquely

Scrollbars : It is used to provide scrolling .It can be vertical or Horizontal.

Width : It is used to provide Width of RecylerView.

Height : It is used to provide Height of RecyclerView.

So after declaration of RecyclerView in Xml let see how it looks in xml.

2

Now we have to follow same steps as like we did in GridView, We have to create Bean, Adapter and Activity for RecyclerView. Lets create Bean first .

Now its time to create an Adapter for RecylerView. RecyclerView Adapter

As we know that Adapter is a bridge between UI and data source, It helps us to fill data in the UI components. It pulls data from database or an array. After pulling data from database or an array, it sends data to adapter view and adapter view send it to view. RecyclerView Adapter extends object class, It is used in RecyclerView.

RecyclerView.Adapter() is public constructors . It have some public methods

1. onCreateViewHolder(ViewGroup parent, int viewType): This method is used to initializes fields which is used for RecyclerView. This method calls to create new ViewHolder. Here you have to initialize your layout, Syntax is like below:

  1. onBindViewHolder(ViewHolder holder,int position) : By the name of this method clearly understood that it is used to binding. Now binding for what, It is used to bind content or data with Views of RecyclerView. Like in TextView, ImageView etc. From here we can set data to our Views by given position. Like below

  1. getItemCount() : It returns the total number of item count which held by adapter or size of arraylist. Like below code:

There are many methods are provided by this adapter but these methods are important part of RecyclerView Adapter.

Check below code which is used for RecyclerView Adapter:

Now We are going to do our final stage that is Activity Part, Here we have to fetch RecyclerView by its id and need to use LayoutManager Like in which LayoutManager you are going to View your RecyclerView.

Final Output of this code will look like below image:

3

In this tutorial we have learn about the Android Recycler View and its application with practical example. I hope you will like this tutorial.