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--><RelativeLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âtools:context="com.neveropen.horizontalrecyclerview.MainActivity"   Âandroid:id="@+id/relativelayout">   Â<!--Recycler View widget-->   Â<android.support.v7.widget.RecyclerView       Âandroid: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> - 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.CardView   Âandroid: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-->   Â<TextView       Âandroid: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> - 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.neveropen.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 AdapterpublicclassAdapter   ÂextendsRecyclerView.Adapter<Adapter.MyView> {   Â// List with String type   ÂprivateList<String> list;   Â// View Holder class which   Â// extends RecyclerView.ViewHolder   ÂpublicclassMyView       ÂextendsRecyclerView.ViewHolder {       Â// Text View       ÂTextView textView;       Â// parameterised constructor for View Holder class       Â// which takes the view as a parameter       ÂpublicMyView(View view)       Â{           Âsuper(view);           Â// initialise TextView with id           ÂtextView = (TextView)view                          Â.findViewById(R.id.textview);       Â}   Â}   Â// Constructor for adapter class   Â// which takes a list of String type   ÂpublicAdapter(List<String> horizontalList)   Â{       Âthis.list = horizontalList;   Â}   Â// Override onCreateViewHolder which deals   Â// with the inflation of the card layout   Â// as an item for the RecyclerView.   Â@Override   ÂpublicMyView onCreateViewHolder(ViewGroup parent,                                    ÂintviewType)   Â{       Â// Inflate item.xml using LayoutInflator       ÂView itemView           Â= LayoutInflater                 Â.from(parent.getContext())                 Â.inflate(R.layout.item,                          Âparent,                          Âfalse);       Â// return itemView       ÂreturnnewMyView(itemView);   Â}   Â// Override onBindViewHolder which deals   Â// with the setting of different data   Â// and methods related to clicks on   Â// particular items of the RecyclerView.   Â@Override   ÂpublicvoidonBindViewHolder(finalMyView holder,                                Âfinalintposition)   Â{       Â// Set the text of each item of       Â// Recycler view with the list items       Âholder.textView.setText(list.get(position));   Â}   Â// Override getItemCount which Returns   Â// the length of the RecyclerView.   Â@Override   ÂpublicintgetItemCount()   Â{       Âreturnlist.size();   Â}} - 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.neveropen.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 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;   ÂintRecyclerViewItemPosition;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       Â// initialisation with id's       ÂrecyclerView           Â= (RecyclerView)findViewById(               ÂR.id.recyclerview);       ÂRecyclerViewLayoutManager           Â=newLinearLayoutManager(               ÂgetApplicationContext());       Â// Set LayoutManager on Recycler View       ÂrecyclerView.setLayoutManager(           ÂRecyclerViewLayoutManager);       Â// Adding items to RecyclerView.       ÂAddItemsToRecyclerViewArrayList();       Â// calling constructor of adapter       Â// with source list as a parameter       Âadapter =newAdapter(source);       Â// Set Horizontal Layout Manager       Â// for Recycler view       ÂHorizontalLayout           Â=newLinearLayoutManager(               ÂMainActivity.this,               ÂLinearLayoutManager.HORIZONTAL,               Âfalse);       ÂrecyclerView.setLayoutManager(HorizontalLayout);       Â// Set adapter on recycler view       ÂrecyclerView.setAdapter(adapter);   Â}   Â// Function to add items in RecyclerView.   ÂpublicvoidAddItemsToRecyclerViewArrayList()   Â{       Â// Adding items to ArrayList       Âsource =newArrayList<>();       Âsource.add("gfg");       Âsource.add("is");       Âsource.add("best");       Âsource.add("site");       Âsource.add("for");       Âsource.add("interview");       Âsource.add("preparation");   Â}}
Output:

