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:
- Step 1: Add the dependency of Recycler View widget in your project
- Latest dependency for Recycler View is:
implementation 'com.android.support:recyclerview-v7:28.0.0'
- Also add the dependency for Card View. Latest dependency for Card View is:
implementation 'com.android.support:cardview-v7:28.0.0'
- Latest dependency for Recycler View is:
- 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:
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><!--Relative Layout--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.geeksforgeeks.horizontalrecyclerview.MainActivity"android:id="@+id/relativelayout"><!--Recycler View widget--><android.support.v7.widget.RecyclerViewandroid:id="@+id/recyclerview"android:scrollbars="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_centerHorizontal="true"/></RelativeLayout>chevron_rightfilter_none - 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:
item.xml
<?xmlversion="1.0"encoding="utf-8"?><!--Card View widget--><android.support.v7.widget.CardViewandroid:id="@+id/cardview"android:layout_width="100dp"android:layout_height="wrap_content"card_view:contentPadding="8dp"card_view:cardCornerRadius="8dp"card_view:cardElevation="8dp"card_view:cardBackgroundColor="#0F9D58"android:layout_margin="1dp"><!--Text View over Card View--><TextViewandroid:id="@+id/textview"android:layout_gravity="center"'android:layout_marginTop="4dp"android:layout_marginBottom="4dp"android:textSize="22dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#fff"/></android.support.v7.widget.CardView>chevron_rightfilter_none - 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 adapter.java:
Adapter.java
packagecom.geeksforgeeks.horizontalrecyclerview;importandroid.support.v7.widget.RecyclerView;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.TextView;importandroid.view.LayoutInflater;importjava.util.List;// The adapter class which// extends RecyclerView AdapterpublicclassAdapterextendsRecyclerView.Adapter<Adapter.MyView> {// List with String typeprivateList<String> list;// View Holder class which// extends RecyclerView.ViewHolderpublicclassMyViewextendsRecyclerView.ViewHolder {// Text ViewTextView textView;// parameterised constructor for View Holder class// which takes the view as a parameterpublicMyView(View view){super(view);// initialise TextView with idtextView = (TextView)view.findViewById(R.id.textview);}}// Constructor for adapter class// which takes a list of String typepublicAdapter(List<String> horizontalList){this.list = horizontalList;}// Override onCreateViewHolder which deals// with the inflation of the card layout// as an item for the RecyclerView.@OverridepublicMyView onCreateViewHolder(ViewGroup parent,intviewType){// Inflate item.xml using LayoutInflatorView itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);// return itemViewreturnnewMyView(itemView);}// Override onBindViewHolder which deals// with the setting of different data// and methods related to clicks on// particular items of the RecyclerView.@OverridepublicvoidonBindViewHolder(finalMyView holder,finalintposition){// Set the text of each item of// Recycler view with the list itemsholder.textView.setText(list.get(position));}// Override getItemCount which Returns// the length of the RecyclerView.@OverridepublicintgetItemCount(){returnlist.size();}}chevron_rightfilter_none - Step 5. Setting up the code for MainActivity.java file for Recycler view
The MainActivity contains RecyclerView. Set Layout Manager on Recycler view using LinearLayoutManager class.
Here is complete code for MainActivity.java:MainActivity.java
packagecom.geeksforgeeks.horizontalrecyclerview;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.support.v7.widget.RecyclerView;importandroid.support.v7.widget.LinearLayoutManager;importandroid.view.View;importandroid.widget.Toast;importjava.util.ArrayList;publicclassMainActivityextendsAppCompatActivity {// Recycler View objectRecyclerView recyclerView;// Array list for recycler view data sourceArrayList<String> source;// Layout ManagerRecyclerView.LayoutManager RecyclerViewLayoutManager;// adapter class objectAdapter adapter;// Linear Layout ManagerLinearLayoutManager HorizontalLayout;View ChildView;intRecyclerViewItemPosition;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// initialisation with id'srecyclerView= (RecyclerView)findViewById(R.id.recyclerview);RecyclerViewLayoutManager=newLinearLayoutManager(getApplicationContext());// Set LayoutManager on Recycler ViewrecyclerView.setLayoutManager(RecyclerViewLayoutManager);// Adding items to RecyclerView.AddItemsToRecyclerViewArrayList();// calling constructor of adapter// with source list as a parameteradapter =newAdapter(source);// Set Horizontal Layout Manager// for Recycler viewHorizontalLayout=newLinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,false);recyclerView.setLayoutManager(HorizontalLayout);// Set adapter on recycler viewrecyclerView.setAdapter(adapter);}// Function to add items in RecyclerView.publicvoidAddItemsToRecyclerViewArrayList(){// Adding items to ArrayListsource =newArrayList<>();source.add("gfg");source.add("is");source.add("best");source.add("site");source.add("for");source.add("interview");source.add("preparation");}}chevron_rightfilter_none
Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to add Bullet list in a RecyclerView in Android?
- How to insert Slide From Bottom animation in RecyclerView in Android
- How to populate RecyclerView with Firebase data using FirebaseUI in Android Studio
- How to create a nested RecyclerView in Android
- How to Create RecyclerView with Multiple ViewType in Android?
- RecyclerView in Android with Example
- Pull to Refresh with RecyclerView in Android with Example
- RecyclerView as Staggered Grid in Android With Example
- RecyclerView using GridLayoutManager in Android With Example
- How to draw Horizontal and Vertical lines in an Android App using XML
- Flutter - Horizontal List
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

