Recycler View in Android Part 1- Introduction In Depth

android_learners_hub
android_learners_hub

               Recycler View in Android

Introduction

Recycler View is advanced version of ListView. Recycler View was introduced in Marshmallow. It makes easy to efficiently display very large set of data. Even Recycler View is more flexible than Listview because list view present data in a single vertical list, but recycler view gives us more option(linear list, grid etc.) . Recycler View arrange these items with the help of layout manager class(discussed below).


As the name implies, Recycler View recycles individual elements so that they can be reused. When user scrolls down the items in screen, Recycler View doesn't destroy the view of upper items. Instead, Recycler View reuses the view for new items that have scrolled onscreen. This reuse vastly improves performance, improving our app's responsiveness and reducing power consumption. Even, it will decrease the pressure on the device by loading as much as data that can be seen on the screen.

Hence, advantage of using Recycler view are:-

1. Improves performance
2. Reducing power consumption
3. Decrease the pressure on the device 
4. Efficiently display very large set of data

 Key classes

key classes


1. Adapter :- To bind the views with data we use Adapter class (as shown in figure).You can define the adapter class by extending RecyclerView.Adapter.

Adapter in Recycler View

Here, in Adapter class I will use 3 methods that are OnCreateViewHolder() , OnBindViewHolder() , getItemcount() and also a constructor of adapter class.

1.OnCreateViewHolder():-This method will be called first whenever adapter class is called. RecyclerView calls this method whenever it needs to create a new ViewHolder. The method is used to create and initialize the ViewHolder.

2. OnBindViewHolder():-RecyclerView calls this method to associate/ bind a ViewHolder with data. The method fetches the appropriate data and uses the data to fill in the view holder's layout. For example, if the RecyclerView displays a list of names(data), the method might find the appropriate name in the list and fill in the view holder's TextView widget.
 
3.getItemcount():- RecyclerView calls this method to get the size of the data.Also, RecyclerView uses this to determine when there are no more items that can be displayed. For example, if the RecyclerView displays a list of names(data), the method might find the total number of names to be displayed .


2. View Holder:- It will hold the reference of view , which it will pass to Oncreate() to create view. The Oncreate() then, pass it to onBind() where the data is binded with  recycler view.  You can define the view holder by extending RecyclerView.ViewHolder or by RecyclerView.Adapter<adapter.view_holder>.

3.Layout manager: To arrange the items or data in the list we use Layout Manager class. Layout managers are all based on the library's Layout Manager abstract class.
The common layouts in it are:-
layout manager



1.Linear  Layout Manager:- It arrange the items in one-dimensional list that is in vertical or horizontal.

2.Grid Layout Manager:-It arrange the items in two-dimensional grid with same width and height in same row or column. 
If the grid is arranged vertically, GridLayoutManager will make all the elements in the given  number of row.
If the grid is arranged horizontally, GridLayoutManager will make all the elements in given number of  column.
  
3.StaggeredGridLayout Manager:- It is very much similar to GridLayoutManager, but it does not require that items in a row have the same height (for vertical grids) or items in the same column have the same width (for horizontal grids). So, in the same column or row we can know arrange the items with different width and height (drawback of grid layout is solved) .


Thanks for reading . This is an Introduction to Recycler View , coding will be covered in the next post(Recycler View in Android Part-2 Coding and Explanation (onlyjavaforall.blogspot.com)). Where ,we will make an app to illustrate the concepts that we discussed today.


android_learners_hub

Recycler View in Android Part-2 Coding and Explanation (onlyjavaforall.blogspot.com)



Comments

Post a Comment