Android RecyclerView

Updated 28 November 2019

Save

RecyclerView

RecyclerView is a successor view group of ListView and GridView.It has  different features from ListView and GridView which makes it powerful.In this blog we will discuss about RecyclerView and its Adapter.

 

Creating New Project
1. In Android Studio, go to File ⇒ New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.

2. Open build.gradle and add recycler view dependency.com.android.support:recyclerview-v7:28.0.0 and rebuild the project.

dependencies {
//…

//RecyclerView
implementation ‘com.android.support:recyclerview-v7:28.0.0’

}

create view RecyclerView in activity_main.xml

 

The Card Layout: Card Layout is treated as an item of RecyclerView.

 person_list.xml

The Data Class: The class that holds information about recyclerView

 

The ViewHolder: ViewHolder holds information about card layout .

 

The Adapter: It is responsible for inflating the view and view holder ,binding data to view . It extends RecyclerView.Adapter .There are three main method of RecyclerView.Adapter which are necessary to override

1.onCreateViewHolder: This method only called when new view created. If views are recycled then there is no need to create new view.

2.onBindViewHolder:It sets data to particular items of the RecyclerView.It called every time when it scrolled

3.getItemCount: which Returns the length of the RecyclerView.

 

LayoutManagers

We use LayoutManager class for specifying position of recyclerView’s item.There are three ways of displaying RecyclerView data.

  1. LinearLayoutManager shows items in a vertical or horizontal scrolling list.
  2. GridLayoutManager shows items in a grid.
  3. StaggeredGridLayoutManager shows items in a staggered grid.

Set Adapter to RecyclerView

we need to set adapter to recyclerView which we created . we pass list in constructor of MyAdapter

recyclerView.setAdapter(new MyAdapter(list));

 

 

author
. . .

Leave a Comment

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


1 comments

  • Yogi
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home