Open In App

Android | Horizontal RecyclerView with Examples

Last Updated : 16 Dec, 2019
Like Article

RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be customized vastly while improving on the efficiency of ListViews and GridViews. This improvement is achieved by recycling the views which are out of the visibility of the user.

For example: if a user scrolled down to a position where the items 4 and 5 are visible; items 1, 2 and 3 would be cleared from the memory to reduce memory consumption.

In this article, we will learn how to create recycler view which can be scrolled in a horizontal direction.

Here are the detailed steps:

  1. Step 1: Add the dependency of Recycler View widget in your project
    • Latest dependency for Recycler View is:
      implementation ''
    • Also add the dependency for Card View. Latest dependency for Card View is:
      implementation ''
  2. Step 2: Setting up the activity_main.xml layout file
    The activity_main.xml layout file consists of:

    • Relative layout which contains the recycler view
    • Recycler view widget

    Here is complete code for activity_main.xml:


    <?xml version="1.0" encoding="utf-8"?>
    <!--Relative Layout-->
        <!--Recycler View widget-->
            android:layout_centerHorizontal="true" />


  3. Step 3: Setting up the item.xml layout file for Recycler view
    The item.xml layout file consists of the layout for an item of Recycler View. Item Layout contains a Card View with Text view in it with some text.
    Here is complete code for item.xml:


    <?xml version="1.0" encoding="utf-8"?>
    <!--Card View widget-->
        <!--Text View over Card View-->


  4. Step 4: Setting up the code for Recycler View Adapter
    The adapter is the main code responsible for RecyclerView. It holds all the important methods dealing with the implementation of RecylcerView. The basic methods for a successful implementation are:

    • onCreateViewHolder,
    • onBindViewHolder,
    • getItemCount

    The adapter is used to set data to Recycler View from Data source.

    Here is complete code for

    package com.geeksforgeeks.horizontalrecyclerview;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.view.LayoutInflater;
    import java.util.List;
    // The adapter class which
    // extends RecyclerView Adapter
    public class Adapter
        extends RecyclerView.Adapter<Adapter.MyView> {
        // List with String type
        private List<String> list;
        // View Holder class which
        // extends RecyclerView.ViewHolder
        public class MyView
            extends RecyclerView.ViewHolder {
            // Text View
            TextView textView;
            // parameterised constructor for View Holder class
            // which takes the view as a parameter
            public MyView(View view)
                // initialise TextView with id
                textView = (TextView)view
        // Constructor for adapter class
        // which takes a list of String type
        public Adapter(List<String> horizontalList)
            this.list = horizontalList;
        // Override onCreateViewHolder which deals
        // with the inflation of the card layout
        // as an item for the RecyclerView.
        public MyView onCreateViewHolder(ViewGroup parent,
                                         int viewType)
            // Inflate item.xml using LayoutInflator
            View itemView
                = LayoutInflater
            // return itemView
            return new MyView(itemView);
        // Override onBindViewHolder which deals
        // with the setting of different data
        // and methods related to clicks on
        // particular items of the RecyclerView.
        public void onBindViewHolder(final MyView holder,
                                     final int position)
            // Set the text of each item of
            // Recycler view with the list items
        // Override getItemCount which Returns
        // the length of the RecyclerView.
        public int getItemCount()
            return list.size();


  5. Step 5. Setting up the code for file for Recycler view
    The MainActivity contains RecyclerView. Set Layout Manager on Recycler view using LinearLayoutManager class.
    Here is complete code for

    package com.geeksforgeeks.horizontalrecyclerview;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    import java.util.ArrayList;
    public class MainActivity extends AppCompatActivity {
        // Recycler View object
        RecyclerView recyclerView;
        // Array list for recycler view data source
        ArrayList<String> source;
        // Layout Manager
        RecyclerView.LayoutManager RecyclerViewLayoutManager;
        // adapter class object
        Adapter adapter;
        // Linear Layout Manager
        LinearLayoutManager HorizontalLayout;
        View ChildView;
        int RecyclerViewItemPosition;
        protected void onCreate(Bundle savedInstanceState)
            // initialisation with id's
                = (RecyclerView)findViewById(
                = new LinearLayoutManager(
            // Set LayoutManager on Recycler View
            // Adding items to RecyclerView.
            // calling constructor of adapter
            // with source list as a parameter
            adapter = new Adapter(source);
            // Set Horizontal Layout Manager
            // for Recycler view
                = new LinearLayoutManager(
            // Set adapter on recycler view
        // Function to add items in RecyclerView.
        public void AddItemsToRecyclerViewArrayList()
            // Adding items to ArrayList
            source = new ArrayList<>();



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads