{"id":292,"date":"2017-03-30T10:02:12","date_gmt":"2017-03-30T10:02:12","guid":{"rendered":"http:\/\/abhiandroid.com\/materialdesign\/?page_id=292"},"modified":"2019-06-14T12:35:24","modified_gmt":"2019-06-14T12:35:24","slug":"recyclerview","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/materialdesign\/recyclerview","title":{"rendered":"RecyclerView Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, RecyclerView is an advanced and flexible version of ListView and GridView. It is a container used for\u00a0displaying large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views. RecyclerView was\u00a0introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-428\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/03\/RecyclerView-in-Android.jpg\" alt=\"RecyclerView in Android\" width=\"587\" height=\"348\" srcset=\"https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/03\/RecyclerView-in-Android.jpg 587w, https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/03\/RecyclerView-in-Android-300x178.jpg 300w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/>This new widget is a big step for displaying data in Material Design because the ListView and GridView are one of the most commonly used UI widget. In RecyclerView android provides a lots of new features which are not present in existing ListView or GridView.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span><\/p>\n<p>In Android, RecyclerView provides an ability to implement the horizontal, vertical and Expandable List. It is mainly used when we have data collections whose elements can change at run time based on user action or any network events. For using this widget we have to specify the Adapter and Layout Manager.<\/p>\n<p><strong><span style=\"color: #008000;\">Basic RecyclerView XML code:<\/span><\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\"abhiandroid.com.recyclerviewexample.MainActivity\"&gt;\r\n    &lt;android.support.v7.widget.RecyclerView\r\n        android:id=\"@+id\/recyclerView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Gradle Dependency to use RecyclerView:<\/strong><\/span><\/p>\n<p>The RecyclerView widget is a part of separate library valid for API 7 level or higher. Add the following dependency in your Gradle build file to use recyclerview.<\/p>\n<p><strong>Gradle Scripts &gt; build.gradle and inside dependencies<\/strong><\/p>\n<pre>dependencies {\r\n...\r\ncompile \"com.android.support:recyclerview-v7:23.0.1\"\r\n}\r\n<\/pre>\n<h4><strong>Need of RecyclerView In Android<\/strong><\/h4>\n<p>RecyclerView uses a ViewHolder for storing the reference of the view for one entry in the RecyclerView. When we use ListView or GridView for displaying custom items then we create a custom xml file and then use it inside our Adapter. For this we create a CustomAdapter class and then extends our Base or any other Adapter in it. In getView() method of our Adapter we inflate the item layout xml file and then give the reference of every view by using the unique id&#8217;s we provide in our xml file . Once finished we pass that view to the ListView, ready to be drawn, but the truth is that ListView and GridView do only half the job of achieving true memory efficiency.<\/p>\n<p>ListView\/GridView\u00a0recycle the item layout but don&#8217;t keep the reference to the layout children, forcing us to call findViewById() for every child of our item layout for every time we call getView(). This issue causes the scrolling or non responsive problem as it frantically tries to grab references to the view&#8217;s we needed.<\/p>\n<p>With the arrival of RecyclerView everything is changed. RecyclerView still uses Adapter to act as Data source but in this we have to create a ViewHolder to keep the reference of View in memory, so when we need a new view it either creates a new ViewHolder object to inflate the layout and hold those references or it recycles one from existing stack.<\/p>\n<hr \/>\n<h4><strong>Components of RecyclerView In Android<\/strong><\/h4>\n<p>Below we define the mainly used components of a RecyclerView.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. Layout Managers:<\/strong><\/span><\/p>\n<p>In Android a\u00a0RecyclerView needs to have a Layout Manager and an Adapter to be instantiated. Layout Manager is a very new concept introduced in RecyclerView for defining the type of Layout which RecyclerView should use. It Contains the references for all the views that are filled by the data of the entry. We can create a Custom Layout Manager by extending RecyclerView.LayoutManager Class but RecyclerView Provides three types of in-built Layout Managers.<\/p>\n<p><strong>Linear Layout Manager &#8211;<\/strong> It is used for displaying the data items in a horizontal or vertical scrolling List<br \/>\n<strong>GridLayoutManager &#8211;<\/strong> It is used to show the items in grid format<br \/>\n<strong>StaggeredGridLayoutManager &#8211;<\/strong> It is used to show the items in staggered Grid.<\/p>\n<p>Below\u00a0we explain each type in details:<\/p>\n<h4><strong>Linear Layout Manager<\/strong><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-326\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/02\/RecyclerView-As-ListView-In-Android-Studio.png\" alt=\"RecyclerView As ListView In Android Studio\" width=\"549\" height=\"320\" srcset=\"https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/02\/RecyclerView-As-ListView-In-Android-Studio.png 549w, https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/02\/RecyclerView-As-ListView-In-Android-Studio-300x175.png 300w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><br \/>\nIt is used for displaying the data items in a horizontal or vertical scrolling List. If we need a list(vertical or horizontal) then we need to use LinearLayoutManager with require orientation. In Simple words we can say that we use the LinearLayoutManager for displaying RecyclerView as a ListView.<\/p>\n<p><span style=\"color: #008000;\"><strong>Public constructor for LinearLayoutManager<\/strong><\/span><\/p>\n<ul>\n<li><strong>LinearLayoutManager(Context context):<\/strong> It is used to create a vertical LinearLayoutManager. In this we need to set only one parameter i.e used to set the context the current Activity.\u00a0<strong>E<\/strong><strong>Example: <\/strong>In below code snippet we show how to use this constructor in Android.<br \/>\n<strong>With Default Vertical Orientation:<\/strong><\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a LinearLayoutManager with default orientation\r\nLinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());\r\nrecyclerView.setLayoutManager(linearLayoutManager); \/\/ set LayoutManager to RecyclerView\r\n<\/pre>\n<p><strong>With Horizontal Orientation:<\/strong><\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a LinearLayoutManager\r\nLinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());\r\nlinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); \/\/ set Horizontal Orientation\r\nrecyclerView.setLayoutManager(linearLayoutManager); \/\/ set LayoutManager to RecyclerView\r\n<\/pre>\n<\/li>\n<li><strong>LinearLayoutManager(Context context, int orientation, boolean reverseLayout):<\/strong> In this first parameter is used to set the current context, second is used to set the Layout Orientation should be vertical or horizontal. By using this constructor we can easily create a horizontal or vertical List. Third parameter is a boolean value when set to true layouts from end to start it means items are arranged from end to start.<br \/>\n<strong>Example:<\/strong> In below code snippet we show how to use this constructor in Android.<\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);\r\n\/\/ set a LinearLayoutManager with default horizontal orientation and false value for reverseLayout to show the items from start to end\r\nLinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);\r\nrecyclerView.setLayoutManager(linearLayoutManager); \/\/ set LayoutManager to RecyclerView\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>For more details read\u00a0<a href=\"\/materialdesign\/recyclerview-as-listview.html\">RecyclerView As ListView With Example<\/a><\/p>\n<h4><strong>GridLayoutManager<\/strong><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-360\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/02\/RecycleView-as-GridView-In-Android-Studio.png\" alt=\"RecyclerView as GridView In Android Studio\" width=\"583\" height=\"350\" srcset=\"https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/02\/RecycleView-as-GridView-In-Android-Studio.png 583w, https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/02\/RecycleView-as-GridView-In-Android-Studio-300x180.png 300w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><br \/>\nIt is used to show the items in grid format. If we need to display items in grid format then we can use GridLayoutManager. In simple words we can say that we use the GridLayoutManager for displaying RecyclerView as a GridView.<br \/>\n<span style=\"color: #008000;\"><strong>Public constructor for GridLayoutManager:<\/strong><\/span><\/p>\n<ul>\n<li><strong>GridLayoutManager (Context context, int spanCount):<\/strong> It is used to create a Vertical GridLayoutManager. In this constructor first parameter is used to set the current context and second parameter is used to set the spanCount means the number of columns in the grid.<br \/>\n<strong>Example:<\/strong> In below code snippet we show how to use this constructor in Android.<br \/>\n<strong>With Default Vertical Orientation:<\/strong><\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a GridLayoutManager with default vertical orientation and 3 number of columns\r\nGridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);\r\nrecyclerView.setLayoutManager(gridLayoutManager); \/\/ set LayoutManager to RecyclerView<\/pre>\n<p><strong>With Horizontal Orientation:<\/strong><\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a GridLayoutManager with 3 number of columns\r\nGridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);\r\ngridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); \/\/ set Horizontal Orientation\r\nrecyclerView.setLayoutManager(gridLayoutManager); \/\/ set LayoutManager to RecyclerView<\/pre>\n<\/li>\n<li><strong>GridLayoutManager (Context context, int spanCount, int orientation, boolean reverseLayout):<\/strong> In this constructor first parameter is used to set the current context, second parameter is used to set the spanCount means the number of columns in the grid, third parameter is used to set the Layout Orientation should be vertical or horizontal and last param is a boolean value when sets to true layout from end to start means grids are arranged from end to start.<br \/>\n<strong>Example:<\/strong> In below code snippet we show how to use this constructor in Android.<\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a GridLayoutManager with 3 number of columns , horizontal gravity and false value for reverseLayout to show the items from start to end\r\nGridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);\r\nrecyclerView.setLayoutManager(gridLayoutManager); \/\/ set LayoutManager to RecyclerView\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>For more details read\u00a0<a href=\"\/materialdesign\/recyclerview-gridview.html\">RecyclerView Using GridLayoutManager With Example<\/a><\/p>\n<h4><strong>StaggeredGridLayoutManager<\/strong><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-407\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/03\/GridView-vs-Staggered-Grid-In-Android-Studio.png\" alt=\"GridView vs Staggered Grid In Android Studio\" width=\"512\" height=\"240\" srcset=\"https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/03\/GridView-vs-Staggered-Grid-In-Android-Studio.png 512w, https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/03\/GridView-vs-Staggered-Grid-In-Android-Studio-300x141.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><br \/>\nIt is used to show the items in staggered Grid.<\/p>\n<p><strong><span style=\"color: #008000;\">Public constructor for StaggeredGridLayoutManager<\/span><\/strong><\/p>\n<ul>\n<li><strong>StaggeredGridLayoutManager(int spanCount, int orientation):<\/strong> it is used to create a StaggeredGridLayoutManager with given parameters. First parameter is used to set spanCount means number of columns if orientation is vertical or number of rows if orientation is horizontal, Second Parameter is used to set the Orientation, it should be vertical or horizontal.<br \/>\n<strong>Example: <\/strong>In below code snippet we show how to use this constructor in Android.<br \/>\n<strong>With Vertical Orientation:<\/strong><\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a StaggeredGridLayoutManager with 3 number of columns and vertical orientation\r\nStaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);\r\nrecyclerView.setLayoutManager(staggeredGridLayoutManager); \/\/ set LayoutManager to RecyclerView\r\n<\/pre>\n<p><strong>With Horizontal Orientation:<\/strong><\/p>\n<pre>\/\/ get the reference of RecyclerView\r\nRecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n\/\/ set a StaggeredGridLayoutManager with 3 number of columns and horizontal orientation\r\nStaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.HORIZONTAL);\r\nrecyclerView.setLayoutManager(staggeredGridLayoutManager); \/\/ set LayoutManager to RecyclerView<\/pre>\n<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>2.ViewHolder:<\/strong> <\/span>ViewHolder is used to store the reference of the View&#8217;s for one entry in the RecyclerView. A ViewHolder is a static inner class in our Adapter which holds references to the relevant view&#8217;s. By using these references our code can avoid time consuming findViewById() method to update the widgets with new data.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. RecyclerView.Adapter:<\/strong><\/span><br \/>\nRecyclerView includes a new kind of Adapter. It\u2019s a similar approach to the ones we already used but with some peculiarities such as a required ViewHolder for handling Views. We will have to override two main methods first one to inflate the view and its viewholder and another one to bind the data to the view. The main good thing in this is that the first method is called only when we really need to create a new view.<\/p>\n<p>In Below code snippet we show how&#8217;s our CustomAdapter looks when we extends RecyclerView.Adapter class in it.<\/p>\n<pre>package abhiandroid.com.recyclerviewexample;\r\nimport android.support.v7.widget.RecyclerView;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.TextView;\r\npublic class CustomAdapter extends RecyclerView.Adapter {\r\n@Override\r\npublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {\r\n\/\/ infalte the item Layout\r\nView v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);\r\n\/\/ set the view's size, margins, paddings and layout parameters\r\nMyViewHolder vh = new MyViewHolder(v); \/\/ pass the view to View Holder\r\nreturn vh;\r\n}\r\n@Override\r\npublic void onBindViewHolder(MyViewHolder holder, int position) {\r\n}\r\n@Override\r\npublic int getItemCount() {\r\nreturn 0;\r\n}\r\npublic class MyViewHolder extends RecyclerView.ViewHolder {\r\nTextView textView;\/\/ init the item view's\r\npublic MyViewHolder(View itemView) {\r\nsuper(itemView);\r\n\r\n\/\/ get the reference of item view's\r\ntextView = (TextView) itemView.findViewById(R.id.textView);\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. ItemAnimator:<\/strong> <\/span>RecyclerView.ItemAnimator will animate ViewGroup modification such as delete, select, add that notify the Adapter. DefaultItemAnimator can be used for providing default Animation and it works quite well.<\/p>\n<p>For more details read\u00a0<a href=\"\/materialdesign\/recyclerview-as-staggered-grid-example.html\">RecyclerView As Staggered Grid with example<\/a><\/p>\n<hr \/>\n<h4><strong>RecyclerView Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of RecyclerView in which we display the list of Person Names with the help of RecyclerView. In this example we are using LinearLayoutManager with vertical orientation to display the items. Firstly we declare a RecyclerView in our XML file and then get the reference of it in our Activity. After that we creates an ArrayList for Person Names and set a LayoutManager and finally we set the Adapter to show the items in RecyclerView. Whenever a user clicks on an item the name of the Person is displayed on the screen with the help of Toast.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/y1bgbc02cx2s9dy\/RecyclerViewExample.zip\/file\" target=\"nofollow\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-392\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/03\/RecyclerView-Example-In-Android-Studio.png\" alt=\"RecyclerView Example In Android Studio\" width=\"331\" height=\"494\" srcset=\"https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/03\/RecyclerView-Example-In-Android-Studio.png 331w, https:\/\/abhiandroid.com\/materialdesign\/wp-content\/uploads\/2017\/03\/RecyclerView-Example-In-Android-Studio-201x300.png 201w\" sizes=\"auto, (max-width: 331px) 100vw, 331px\" \/><br \/>\n<span style=\"color: #008000;\"><strong>Step 1:<\/strong> <\/span>Create a new Project and name it RecyclerView Example.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open <span style=\"color: #008000;\">Gradle Scripts &gt; build.gradle<\/span> and add RecyclerView Library dependency in it.<\/p>\n<pre>apply plugin: 'com.android.application'\r\nandroid {\r\n    compileSdkVersion 24\r\n    buildToolsVersion \"24.0.1\"\r\n    defaultConfig {\r\n        applicationId \"abhiandroid.com.recyclerviewexample\"\r\n        minSdkVersion 16\r\n        targetSdkVersion 24\r\n        versionCode 1\r\n        versionName \"1.0\"\r\n    }\r\n    buildTypes {\r\n        release {\r\n            minifyEnabled false\r\n            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'\r\n        }\r\n    }\r\n}\r\ndependencies {\r\n    compile fileTree(dir: 'libs', include: ['*.jar'])\r\n    testCompile 'junit:junit:4.12'\r\n    compile 'com.android.support:appcompat-v7:24.1.1'\r\n    compile \"com.android.support:recyclerview-v7:23.0.1\" \/\/ dependency file for RecyclerView\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong> <\/span>Open<span style=\"color: #008000;\"> res -&gt; layout -&gt; activity_main.xml (or) main.xml<\/span> and add following code:<\/p>\n<p>In this step we create a RecyclerView in our XML file.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\"abhiandroid.com.recyclerviewexample.MainActivity\"&gt;\r\n    &lt;android.support.v7.widget.RecyclerView\r\n        android:id=\"@+id\/recyclerView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Create a new XML file <span style=\"color: #008000;\">rowlayout.xml<\/span> for item of RecyclerView and paste the following code in it.<\/p>\n<p>In this step we create a new xml file for item row in which we creates a TextView to show the data.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:orientation=\"vertical\"&gt;\r\n    &lt;!--\r\n    items for a single row of RecyclerView\r\n    --&gt;\r\n    &lt;TextView\r\n        android:id=\"@+id\/name\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_margin=\"10dp\"\r\n        android:textColor=\"#000\"\r\n        android:textSize=\"20sp\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 5 :<\/strong><\/span> Now open <span style=\"color: #008000;\">app -&gt; java -&gt; package -&gt; MainActivity.java<\/span> and add the below code.<\/p>\n<p>In this step firstly we get the reference of RecyclerView. After that we creates an ArrayList for Person Names and set a LayoutManager and finally we set the Adapter to show the items in RecyclerView.<\/p>\n<pre>package abhiandroid.com.recyclerviewexample;\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.support.v7.widget.LinearLayoutManager;\r\nimport android.support.v7.widget.RecyclerView;\r\nimport java.util.ArrayList;\r\nimport java.util.Arrays;\r\npublic class MainActivity extends AppCompatActivity {\r\n    \/\/ ArrayList for person names\r\n    ArrayList personNames = new ArrayList&lt;&gt;(Arrays.asList(\"Person 1\", \"Person 2\", \"Person 3\", \"Person 4\", \"Person 5\", \"Person 6\", \"Person 7\"));\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ get the reference of RecyclerView\r\n        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n        \/\/ set a LinearLayoutManager with default vertical orientation\r\n        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());\r\n        recyclerView.setLayoutManager(linearLayoutManager);\r\n        \/\/  call the constructor of CustomAdapter to send the reference and data to Adapter\r\n        CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, personNames);\r\n        recyclerView.setAdapter(customAdapter); \/\/ set the Adapter to RecyclerView\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 6:<\/strong><\/span> Create a new class CustomAdapter.java inside package and add the following code.<\/p>\n<p>In this step we create a CustomAdapter class and extends RecyclerView.Adapter class with ViewHolder in it. After that we implement the overrided methods and create a constructor for getting the data from Activity, In this custom Adapter two methods are more important first is onCreateViewHolder in which we inflate the layout item xml and pass it to View Holder and other is onBindViewHolder in which we set the data in the view&#8217;s with the help of ViewHolder.<\/p>\n<pre>package abhiandroid.com.recyclerviewexample;\r\nimport android.content.Context;\r\nimport android.support.v7.widget.RecyclerView;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.TextView;\r\nimport android.widget.Toast;\r\nimport java.util.ArrayList;\r\npublic class CustomAdapter extends RecyclerView.Adapter {\r\n    ArrayList personNames;\r\n    Context context;\r\n    public CustomAdapter(Context context, ArrayList personNames) {\r\n        this.context = context;\r\n        this.personNames = personNames;\r\n    }\r\n    @Override\r\n    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {\r\n        \/\/ infalte the item Layout\r\n        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);\r\n        \/\/ set the view's size, margins, paddings and layout parameters\r\n        MyViewHolder vh = new MyViewHolder(v); \/\/ pass the view to View Holder\r\n        return vh;\r\n    }\r\n    @Override\r\n    public void onBindViewHolder(MyViewHolder holder, final int position) {\r\n        \/\/ set the data in items\r\n        holder.name.setText(personNames.get(position));\r\n        \/\/ implement setOnClickListener event on item view.\r\n        holder.itemView.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                \/\/ display a toast with person name on item click\r\n                Toast.makeText(context, personNames.get(position), Toast.LENGTH_SHORT).show();\r\n            }\r\n        });\r\n    }\r\n    @Override\r\n    public int getItemCount() {\r\n        return personNames.size();\r\n    }\r\n    public class MyViewHolder extends RecyclerView.ViewHolder {\r\n        TextView name;\/\/ init the item view's\r\n        public MyViewHolder(View itemView) {\r\n            super(itemView);\r\n            \/\/ get the reference of item view's\r\n            name = (TextView) itemView.findViewById(R.id.name);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and you will see different names of person listed in ListView.<\/p>\n<p><strong>For More Example Of RecyclerView Read:<\/strong><\/p>\n<ul>\n<li><a href=\"\/materialdesign\/recyclerview-as-staggered-grid-example.html\">RecyclerView As Staggered Grid Example<\/a><\/li>\n<li><a href=\"\/materialdesign\/recyclerview-gridview.html\">RecyclerView As GridLayoutManager With Example<\/a><\/li>\n<li><a href=\"\/materialdesign\/recyclerview-as-listview.html\">RecyclerView As\u00a0ListView Example<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In Android, RecyclerView is an advanced and flexible version of ListView and GridView. It is a container used for\u00a0displaying large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views. RecyclerView was\u00a0introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop). This new widget is a &hellip; <a href=\"https:\/\/abhiandroid.com\/materialdesign\/recyclerview\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RecyclerView Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":428,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-292","page","type-page","status-publish","has-post-thumbnail","hentry"],"psp_head":"<title>RecyclerView Tutorial With Example In Android Studio \u2013 Android Material Design Tutorial<\/title>\r\n<meta name=\"description\" content=\"Complete tutorial to teach you the concept of RecyclerView with example in Android Studio using LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/materialdesign\/recyclerview\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages\/292\/revisions"}],"predecessor-version":[{"id":752,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages\/292\/revisions\/752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/media\/428"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/media?parent=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}