Android Working with Recyclerview and Cardview

Android Working with Recyclerview and Cardview.Hello guys, i am back with another tutorial on Recyclerview. RecyclerView and CardView got introduced in android Lollipop. Basically The RecyclerView widget is a more advanced and flexible version of ListView. When we need to display a scrolling list of elements based on large data sets we use Recyclerview. As the name suggests, anrdoid RecyclerView is used to reuse cells when scrolling up and down by recycling the items in the list.

Android CardView UI component shows information inside cards. This component is generally used to show contact information. In this tutorial we will show some information inside the CardView using RecyclerView.

So lets start..

Android Working with Recyclerview and Cardview

Lets start by creating a new project i am giving it name RecyclerViewTest.

Creating Android Project

  • Create a new project in Android Studio File–>New–>New Project.
  • Enter Application Name, Package Name, Select Empty Activity and click on finish.

Adding RecyclerView and CardView

  • Now we will add RecyclerView and CardView to our project by adding their Dependencies on our project.
  • Add Dependencies on gradle.

  • Now sync your project.

Creating RecyclerView Layouts

RecyclerView

  • Create a RecyclerView inside your activity_main.xml.
  • Give it a id myRecyclerView.

RecyclerView Item Layout using CardView

  • We need to create separate layout for for items for RecyclerView.
  • For that we will use CardView.
  • So we can create a desired design inside CardView.
  • CardView is not mandatory. CardView gives a nice elevated look that’s why we are using it in our project.
  • In our project we are simply displaying one Heading and one Description using TextView.
  • Create a new layout resource file named list_item.xml and use the following xml code.

Creating Model Class

  • Here we don’t have a single item for our list, and that is why it can’t be stored in a String array. We need a class to store all the attributes that we have for item in our list.
  • In our project we have Heading and Description as list item.
  • So to store these attributes we need to create a class let’s name it ListItem.java.
  • Write the following code.

Creating RecyclerView Adapter

  • To manage and display the data of our RecyclerView we need a class that will extend RecyclerView.Adapter. Inside this class we need RecyclerView.ViewHolder. Now what are these two?
  • RecyclerView.ViewHolder represents the views of our RecyclerView and the RecyclerView.Adapter represents the data that is to be shown with the ViewHoder.
  • We have 3 methods inside RecyclerView.Adapter that we need to override.
    1. RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
      • This method returns a new instance of our ViewHolder.
    2. void onBindViewHolder(ProductViewHolder holder, int position)
      • This method binds the data to the view holder.
    3. int getItemCount()
      • This returns the size of the List.
  • So lets create our Adapter. For this create a new class named MyAdapter.java and write the following code.

Setting Adapter to RecyclerView

  • The last step to show RecyclerView. Come inside MainActivity and write the following code.

RECENT POST

Leave a Reply

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