Android Horizontal RecyclerView CardView And Images Example

Android Horizontal RecyclerView CardView And Images Example Tutorial is written here.

Generally, we create vertical recyclerview to show the data in tabular manner.

In this tutorial, we will implement a recyclerview with the horizontal variant instead of vertical.

By default, recyclerview is vertical but android provides a built in method to make recyclerview horizontal.

While we do not have any such method in ListView. So it is easier to make horizontal recyclerview than horizontal listview.

Of course we get same functionality if we make horizontal listview but it is little complex task.

Look of horizontal Recyclerview

If you want to create a horizontal listview then

read how to implement horizontal listview in android.

Follow all the below steps to make horizontal recyclerview.

Step 1. Making Dependencies

To implement recyclerview and cardview, we need to add dependencies separately.

Write the following lines in build.gradle(Module: app)

These lines will fetch required classes to use recyclerview and cardview in your android app.

Step 2. Special Layout File

In recyclerview, we need to create additional layout xml file.

  • This file will create a view for every single row item. Adapter class will inflate this file while generating the recyclerview row items.
  • Make a new file res->layout structure. Give it a name as recycler_item.xml

Copy the following source code in recycler_item.xml

  • As we want to make a cardview, here I have set it in the above file.
  • Cardview is the parent of all the other views.
  • Then inside cardview, I have taken one Linearlayout with vertical orientation.
  • Inside this linearlayout, one imageview and one textview is present. Imageview will be above the textview.

Downloading Images

We will set fruit images in our horizontal recyclerview.

You can download the required fruit images by clicking the below link.

Download fruit Images

After downloading the images, copy them into the res->drawable directory.

Step 3. Fruit Model

Now let us create a model class. This class is useful to maintain proper data structure.

Create a new class named “” and add the following code in it.

  • This class includes the getter ans setter methods for all the UI widgets which are present in the every row of the recyclerview.
  • In this example, we have one imageview and textview each, so this model have methods for image and text as you can show in the above code.

Step 4. Fruit Adapter

Adapter class will use the data and will create a recyclerview.

Prepare a new class named “”

Write down the below coding lines in “”

Looking Heavily At Adapter

Let us understand the below code

  • Above is the constructor of FruitAdapter class. It has two parameters. First parameter will get context and second will get arraylist (imageModelArraList) of the objects of the FruitModel class.
  • This arraylist imageModelArraList will provide appropriate data to the adapter class.

 onBindViewHolder() method

Below is the code for onBindViewHolder() method.

  • This method will set text in textview and image in the imageview.
  • It will use that arraylist (imageModelArraList ) to fetch the data.
  • Compiler will call this method for number of time which is equal to the number of rows in the recyclerview.

Step 5. Last Modifications

Last but not least, make changes in the activity_main.xml and file.

Copy the following code snippet in activity_main.xml

  • In the main layout file, I have write only recyclerview code.

Code snippet for will be as below

How does works ?

Consider the following source code

  • Line one is creating an object of the RecyclerView class.
  • Second line is making an arraylist with the objects of the FruitModel class. Third will create an object of FruitAdapter class.
  • Fourth line will create one integer array named myImageList. This myImageList contains as integer reference of the fruit images present in the drawable folder.
  • Fifth line makes a string array myImageNameList.

myImageNameList holds the names of the fruits.

Attend the following code

  • Above line is populating an arraylist using the eatFruits method.

Below is the code for eatFruits() methods.

  • This method will rotate a for loop with seven iterations.
  • During every iterations, compiler will create an object of the FruitModel class.
  • Then it will set the image integer reference and image name to that object.
  • After that compiler will add this object in to the arraylist and this process continues for seven times.

Read the following coding lines

  • Compiler will create an object of FruitAdapter class with required parameters during first line.
  • By reading the second line, system will set the adapter with the recyclerview.
  • Now third line is the most important for us.
  • It will set the layout to the recyclerview. We are telling compiler to make our recyclerview horizontal in this line. 

So, now if you run your project, you should get the output like the video you watched at the starting of the tutorial.

 Download Source Code For Android Horizontal RecyclerView

[sociallocker]Download Source Code For Recyclerview_Horizontal[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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