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
<?
xml
version
=
"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
<?
xml
version
=
"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
package
com.neveropen.horizontalrecyclerview;
Â
Âimport
android.support.v7.widget.RecyclerView;
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)
       Â
{
           Â
super
(view);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// initialise TextView with id
           Â
textView = (TextView)view
                          Â
.findViewById(R.id.textview);
       Â
}
   Â
}
Â
ÂÂ Â Â Â
// 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.
   Â
@Override
   Â
public
MyView onCreateViewHolder(ViewGroup parent,
                                    Â
int
viewType)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Inflate item.xml using LayoutInflator
       Â
View itemView
           Â
= LayoutInflater
                 Â
.from(parent.getContext())
                 Â
.inflate(R.layout.item,
                          Â
parent,
                          Â
false
);
Â
ÂÂ Â Â Â Â Â Â Â
// 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.
   Â
@Override
   Â
public
void
onBindViewHolder(
final
MyView holder,
                                Â
final
int
position)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// 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
   Â
public
int
getItemCount()
   Â
{
       Â
return
list.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
package
com.neveropen.horizontalrecyclerview;
Â
Âimport
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.support.v7.widget.RecyclerView;
import
android.support.v7.widget.LinearLayoutManager;
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;
Â
ÂÂ Â Â Â
@Override
   Â
protected
void
onCreate(Bundle savedInstanceState)
   Â
{
       Â
super
.onCreate(savedInstanceState);
       Â
setContentView(R.layout.activity_main);
Â
ÂÂ Â Â Â Â Â Â Â
// initialisation with id's
       Â
recyclerView
           Â
= (RecyclerView)findViewById(
               Â
R.id.recyclerview);
       Â
RecyclerViewLayoutManager
           Â
=
new
LinearLayoutManager(
               Â
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 =
new
Adapter(source);
Â
ÂÂ Â Â Â Â Â Â Â
// Set Horizontal Layout Manager
       Â
// for Recycler view
       Â
HorizontalLayout
           Â
=
new
LinearLayoutManager(
               Â
MainActivity.
this
,
               Â
LinearLayoutManager.HORIZONTAL,
               Â
false
);
       Â
recyclerView.setLayoutManager(HorizontalLayout);
Â
ÂÂ Â Â Â Â Â Â Â
// Set adapter on recycler view
       Â
recyclerView.setAdapter(adapter);
   Â
}
Â
ÂÂ Â Â Â
// Function to add items in RecyclerView.
   Â
public
void
AddItemsToRecyclerViewArrayList()
   Â
{
       Â
// Adding items to ArrayList
       Â
source =
new
ArrayList<>();
       Â
source.add(
"gfg"
);
       Â
source.add(
"is"
);
       Â
source.add(
"best"
);
       Â
source.add(
"site"
);
       Â
source.add(
"for"
);
       Â
source.add(
"interview"
);
       Â
source.add(
"preparation"
);
   Â
}
}
Output: