{"id":524,"date":"2017-07-28T05:22:10","date_gmt":"2017-07-28T05:22:10","guid":{"rendered":"http:\/\/abhiandroid.com\/materialdesign\/?page_id=524"},"modified":"2019-06-13T10:52:21","modified_gmt":"2019-06-13T10:52:21","slug":"pulltorefresh","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/materialdesign\/pulltorefresh","title":{"rendered":"Pull To Refresh ListView &#038; RecyclerView Example In Android Studio &#8211; SwipeRefreshLayout"},"content":{"rendered":"<p>In Android app Pull To Refresh aka SwipeRefreshLayout is used whenever we need to refresh the content&#8217;s of a view via a vertical swipe gesture. It accepts only one child means the component we want to refresh. It uses the listener mechanism to inform the listener who holds this component that a refresh event has occurred.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-532\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/07\/Basic-Swipe-Refresh-Layout-Example-Pull-to-Refresh.gif\" alt=\"Basic Swipe Refresh Layout Example - Pull to Refresh\" width=\"215\" height=\"325\" \/>The Activity which instantiates SwipeRefreshLayout view should add an OnRefreshListener to be notified when the swipe to refresh gesture is completed. The Activity is responsible to handle the refresh event and refreshing the View. If the listener determines that there should not be a refresh then it must call setRefreshing(false) to cancel the visual indication of a refresh. If an Activity wished to show a progress animation then it should call setRefreshing(true) method to enable the gesture and progress animation.<\/p>\n<p>Implementing a pull to refresh is very easy in Android. Whenever we need to detect the swipe down on any view, just wrap the view around SwipeRefreshLayout element.<\/p>\n<hr \/>\n<h4>Need of Pull To Refresh\/SwipeRefreshLayout In Android:<\/h4>\n<p>Now a days lot of Android Apps like Google+, twitter etc provides an option to swipe or pull down to refresh the content of the page. When we swipe from top to down a loader will be displayed and will disappears once the new content is loaded.<\/p>\n<p>Earlier we used to implement a custom swipe view to detect the swipe down but now android have made our work easier by introducing SwipeRefreshLayout in android.support.v4 to detect the vertical swipe on any view. It is mainly used with ListView or RecyclerView where we have a list of data fetched from server and we need to refresh it to get new records.<\/p>\n<hr \/>\n<h4><strong>Basic Pull To Refresh \/ SwipeRefreshLayout XML code:<\/strong><\/h4>\n<pre>&lt;android.support.v4.widget.SwipeRefreshLayout\r\n        android:id=\"@+id\/simpleSwipeRefreshLayout\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"&gt;\r\n\r\n        &lt; Add View's Here....\/&gt;\r\n\r\n&lt;\/android.support.v4.widget.SwipeRefreshLayout&gt;\r\n\r\n<\/pre>\n<hr \/>\n<h4><strong>Example 1:\u00a0Basic Example Of Pull To Refresh In Android Studio:<\/strong><\/h4>\n<p>Below is the first example of SwipeRefreshLayout in which we display a random number in our TextView. Firstly we declare a SwipeRefreshLayout and a TextView in our XML file then get the reference of both in our Activity. After that we implement setOnRefreshListener event on SwipeRefreshLayout and in onRefresh() method we generate a random number and display it in our TextView.<\/p>\n<p>Below you can download code, see final output and step by step explanation of the basic Pull To refresh example.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/SwipeRefreshLayoutExample\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><\/p>\n<p><span style=\"color: #008000;\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-532\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/07\/Basic-Swipe-Refresh-Layout-Example-Pull-to-Refresh.gif\" alt=\"Basic Swipe Refresh Layout Example - Pull to Refresh\" width=\"215\" height=\"325\" \/>Step 1:<\/strong><\/span> Create A New Project And name It SwipeRefreshLayoutExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong> <\/span>Open Gradle Scripts &gt; build.gradle and ensure support library is defined in it or not.<\/p>\n<pre>apply plugin: 'com.android.application'\r\n\r\nandroid {\r\n    compileSdkVersion 25\r\n    buildToolsVersion \"25.0.3\"\r\n    defaultConfig {\r\n        applicationId \"abhiandroid.com.swiperefreshlayoutexample\"\r\n        minSdkVersion 15\r\n        targetSdkVersion 25\r\n        versionCode 1\r\n        versionName \"1.0\"\r\n        testInstrumentationRunner \"android.support.test.runner.AndroidJUnitRunner\"\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\n\r\ndependencies {\r\n    compile fileTree(dir: 'libs', include: ['*.jar'])\r\n    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {\r\n        exclude group: 'com.android.support', module: 'support-annotations'\r\n    })\r\n    compile 'com.android.support:appcompat-v7:25.3.1'\r\n    compile 'com.android.support.constraint:constraint-layout:1.0.2'\r\n    testCompile 'junit:junit:4.12'\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code:<br \/>\nIn this step we create a SwipeRefreshLayout and two TextView&#8217;s in our XML file.<\/p>\n<pre> &lt;android.support.v4.widget.SwipeRefreshLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:id=\"@+id\/simpleSwipeRefreshLayout\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\"\r\n        android:orientation=\"horizontal\"\r\n        android:padding=\"20dp\"&gt;\r\n\r\n        &lt;TextView\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:text=\"Random Number: \"\r\n            android:textColor=\"#000\"\r\n            android:textSize=\"20sp\" \/&gt;\r\n\r\n        &lt;TextView\r\n            android:id=\"@+id\/textView\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:textColor=\"#000\"\r\n            android:textSize=\"20sp\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n&lt;\/android.support.v4.widget.SwipeRefreshLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> \u00a0Now open app -&gt; java -&gt; package -&gt; MainActivity.java and add the below code.<\/p>\n<p>In this step firstly we get the reference of SwipeRefreshLayout and TextView. After that we implement setOnRefreshListener event on SwipeRefreshLayout and in onRefresh() method we generate a random number and display it in our TextView.<\/p>\n<pre>package abhiandroid.com.swiperefreshlayoutexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.os.Handler;\r\nimport android.support.v4.widget.SwipeRefreshLayout;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.Random;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    SwipeRefreshLayout swipeRefreshLayout;\r\n    TextView textView;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ init SwipeRefreshLayout and TextView\r\n        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.simpleSwipeRefreshLayout);\r\n        textView = (TextView) findViewById(R.id.textView);\r\n        \/\/ implement setOnRefreshListener event on SwipeRefreshLayout\r\n        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {\r\n            @Override\r\n            public void onRefresh() {\r\n\r\n                \/\/ implement Handler to wait for 3 seconds and then update UI means update value of TextView\r\n                new Handler().postDelayed(new Runnable() {\r\n                    @Override\r\n                    public void run() {\r\n                        \/\/ cancle the Visual indication of a refresh\r\n                        swipeRefreshLayout.setRefreshing(false);\r\n                        \/\/ Generate a random integer number\r\n                        Random r = new Random();\r\n                        int i1 = r.nextInt(80 - 65) + 65;\r\n                        \/\/ set the number value in TextView\r\n                        textView.setText(String.valueOf(i1));\r\n                    }\r\n                }, 3000);\r\n            }\r\n        });\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and pull down to refresh the random number.<\/p>\n<h4><strong>Example 2: Pull To Refresh with ListView In Android Studio:<\/strong><\/h4>\n<p>Below is the second example of SwipeRefreshLayout with ListView in which we display list of Items and shuffle them on top to down swipe. Firstly we declare a SwipeRefreshLayout and a ListView in our XML file then get the reference of both in our Activity. After that we create a String type list of elements and then implement ArrayAdapter to set data in the list. Finally we implement setOnRefreshListener event on SwipeRefreshLayout and in onRefresh() method we shuffle the list items and set the adapter.<\/p>\n<p>Below you can download code, see final output and step by step explanation of the Pull to refresh example with ListView.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/SwipeRefreshLayoutExample\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><\/p>\n<p><span style=\"color: #008000;\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-534\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/07\/Swipe-Refresh-Layout-With-ListView-Example-Android-Studio.gif\" alt=\"Swipe Refresh Layout With ListView Example Android Studio\" width=\"211\" height=\"301\" \/>Step 1:<\/strong><\/span> Create A New Project And Name It SwipeRefreshLayoutExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong> <\/span>Open Gradle Scripts &gt; build.gradle and ensure support library is defined in it or not.<\/p>\n<pre> apply plugin: 'com.android.application'\r\n\r\nandroid {\r\n    compileSdkVersion 25\r\n    buildToolsVersion \"25.0.3\"\r\n    defaultConfig {\r\n        applicationId \"abhiandroid.com.swiperefreshlayoutexample\"\r\n        minSdkVersion 15\r\n        targetSdkVersion 25\r\n        versionCode 1\r\n        versionName \"1.0\"\r\n        testInstrumentationRunner \"android.support.test.runner.AndroidJUnitRunner\"\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\n\r\ndependencies {\r\n    compile fileTree(dir: 'libs', include: ['*.jar'])\r\n    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {\r\n        exclude group: 'com.android.support', module: 'support-annotations'\r\n    })\r\n    compile 'com.android.support:appcompat-v7:25.3.1'\r\n    compile 'com.android.support.constraint:constraint-layout:1.0.2'\r\n    testCompile 'junit:junit:4.12'\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code:<br \/>\nIn this step we create a SwipeRefreshLayout and a ListView in our XML file.<\/p>\n<pre> &lt;android.support.v4.widget.SwipeRefreshLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:id=\"@+id\/simpleSwipeRefreshLayout\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\"\r\n        android:orientation=\"horizontal\"\r\n        android:padding=\"20dp\"&gt;\r\n\r\n        &lt;ListView\r\n            android:id=\"@+id\/listView\"\r\n            android:layout_width=\"match_parent\"\r\n            android:layout_height=\"match_parent\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n&lt;\/android.support.v4.widget.SwipeRefreshLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Now open app -&gt; java -&gt; package -&gt; MainActivity.java and add the below code.<\/p>\n<p>In this step firsly we get the reference of SwipeRefreshLayout and ListView. After that we create a String type list of elements and then implement ArrayAdapter to set data in the list. Finally we implement setOnRefreshListener event on SwipeRefreshLayout and in onRefresh() method we shuffle the list items and set the adapter..<\/p>\n<pre> package abhiandroid.com.swiperefreshlayoutexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v4.widget.SwipeRefreshLayout;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.Arrays;\r\nimport java.util.Collections;\r\nimport java.util.Random;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    SwipeRefreshLayout swipeRefreshLayout;\r\n    ListView listView;\r\n    ArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;(Arrays.asList(\"First Element\", \"Second Element\", \"Third Element\", \"Fourth Element\", \"Fifth Element\"));\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ init SwipeRefreshLayout and ListView\r\n        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.simpleSwipeRefreshLayout);\r\n        listView = (ListView) findViewById(R.id.listView);\r\n        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);\r\n        listView.setAdapter(adapter);\r\n        \/\/ implement setOnRefreshListener event on SwipeRefreshLayout\r\n        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {\r\n            @Override\r\n            public void onRefresh() {\r\n                \/\/ cancel the Visual indication of a refresh\r\n                swipeRefreshLayout.setRefreshing(false);\r\n                shuffleItems();\r\n            }\r\n        });\r\n    }\r\n\r\n    public void shuffleItems() {\r\n        \/\/ shuffle the ArrayList items and set the adapter\r\n        Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));\r\n        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);\r\n        listView.setAdapter(adapter);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and pull down to refresh the data in ListView.<\/p>\n<h4><strong>Example 3: Pull To Refresh with RecyclerView In Android Studio:<\/strong><\/h4>\n<p>Below is the example of SwipeRefreshLayout with RecyclerView in which we display list of Items and shuffle them on top to down swipe. Firstly we declare a SwipeRefreshLayout and a RecyclerView in our XML file then get the reference of both in our Activity. After that we create a String type list of elements and then implement RecyclerView Adapter to set data in the list. Finally we implement setOnRefreshListener event on SwipeRefreshLayout and in onRefresh() method we shuffle the list items and set the adapter.<\/p>\n<p>Below you can download code, see final output and step by step explanation of the Pull to refresh example with RecyclerView.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/SwipeRefreshLayoutExample\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><\/p>\n<p><span style=\"color: #008000;\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-536\" src=\"\/materialdesign\/wp-content\/uploads\/2017\/07\/SwipeRefreshLayout-With-RecyclerView-Example-Android-Studio.gif\" alt=\"SwipeRefreshLayout With RecyclerView Example Android Studio\" width=\"208\" height=\"334\" \/>Step 1:<\/strong> <\/span>Create A New Project And Name It SwipeRefreshLayoutExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open Gradle Scripts &gt; build.gradle and add RecyclerView Dependency and also ensure support library is defined in it or not.<\/p>\n<pre> apply plugin: 'com.android.application'\r\n\r\nandroid {\r\n    compileSdkVersion 25\r\n    buildToolsVersion \"25.0.3\"\r\n    defaultConfig {\r\n        applicationId \"abhiandroid.com.swiperefreshlayoutexample\"\r\n        minSdkVersion 15\r\n        targetSdkVersion 25\r\n        versionCode 1\r\n        versionName \"1.0\"\r\n        testInstrumentationRunner \"android.support.test.runner.AndroidJUnitRunner\"\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\n\r\ndependencies {\r\n    compile fileTree(dir: 'libs', include: ['*.jar'])\r\n    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {\r\n        exclude group: 'com.android.support', module: 'support-annotations'\r\n    })\r\n    compile 'com.android.support:appcompat-v7:25.3.1'\r\n    compile 'com.android.support.constraint:constraint-layout:1.0.2'\r\n    compile \"com.android.support:recyclerview-v7:23.0.1\" \/\/ dependency file for RecyclerView\r\n    testCompile 'junit:junit:4.12'\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong> <\/span>Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code:<br \/>\nIn this step we create a SwipeRefreshLayout and a RecyclerView in our XML file.<\/p>\n<pre> &lt;android.support.v4.widget.SwipeRefreshLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:id=\"@+id\/simpleSwipeRefreshLayout\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n\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;\/android.support.v4.widget.SwipeRefreshLayout&gt;\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 4:<\/span><\/strong> Create a new XML file rowlayout.xml for item of RecyclerView and paste the following code in it.<br \/>\nIn this step we create a new xml file for item row in which we creates a TextView and ImageView 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=\"horizontal\"\r\n    android:padding=\"5dp\"&gt;\r\n    &lt;!--\r\n    items for a single row of RecyclerView\r\n    --&gt;\r\n    &lt;ImageView\r\n        android:id=\"@+id\/image\"\r\n        android:layout_width=\"70dp\"\r\n        android:layout_height=\"70dp\"\r\n        android:scaleType=\"fitXY\"\r\n        android:src=\"@mipmap\/ic_launcher\" \/&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_gravity=\"center_vertical\"\r\n        android:layout_marginLeft=\"10dp\"\r\n        android:text=\"ABCD\"\r\n        android:textColor=\"#000\"\r\n        android:textSize=\"20sp\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span> Now open app -&gt; java -&gt; package -&gt; MainActivity.java and add the below code.<\/p>\n<p>In this step firsly we get the reference of SwipeRefreshLayout and RecyclerView. After that we creates two ArrayList\u2018s for Person Names and then we set a LayoutManager and finally we set the Adapter to show the items in RecyclerView. Finally we implement setOnRefreshListener event on SwipeRefreshLayout and in onRefresh() method we shuffle the list items and set the adapter.<\/p>\n<pre> package abhiandroid.com.swiperefreshlayoutexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v4.widget.SwipeRefreshLayout;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.support.v7.widget.LinearLayoutManager;\r\nimport android.support.v7.widget.RecyclerView;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.Arrays;\r\nimport java.util.Collections;\r\nimport java.util.Random;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    SwipeRefreshLayout swipeRefreshLayout;\r\n    RecyclerView recyclerView;\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\", \"Person 8\", \"Person 9\", \"Person 10\", \"Person 11\", \"Person 12\", \"Person 13\", \"Person 14\"));\r\n    ArrayList personImages = new ArrayList&lt;&gt;(Arrays.asList(R.drawable.person1, R.drawable.person2, R.drawable.person3, R.drawable.person4, R.drawable.person5, R.drawable.person6, R.drawable.person7, R.drawable.person1, R.drawable.person2, R.drawable.person3, R.drawable.person4, R.drawable.person5, R.drawable.person6, R.drawable.person7));\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ init SwipeRefreshLayout and ListView\r\n        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.simpleSwipeRefreshLayout);\r\n        \/\/ get the reference of RecyclerView\r\n        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, personImages);\r\n        recyclerView.setAdapter(customAdapter); \/\/ set the Adapter to RecyclerView\r\n        \/\/ implement setOnRefreshListener event on SwipeRefreshLayout\r\n        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {\r\n            @Override\r\n            public void onRefresh() {\r\n                \/\/ cancel the Visual indication of a refresh\r\n                swipeRefreshLayout.setRefreshing(false);\r\n                shuffleItems();\r\n            }\r\n        });\r\n    }\r\n\r\n    public void shuffleItems() {\r\n        \/\/ shuffle the ArrayList's items and set the adapter\r\n        Collections.shuffle(personNames, new Random(System.currentTimeMillis()));\r\n        Collections.shuffle(personImages, new Random(System.currentTimeMillis()));\r\n        \/\/ call the constructor of CustomAdapter to send the reference and data to Adapter\r\n        CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, personNames, personImages);\r\n        recyclerView.setAdapter(customAdapter); \/\/ set the Adapter to RecyclerView\r\n    }\r\n}<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 6:<\/span> <\/strong>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 View Holder 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\u2019s with the help of View Holder. Finally we implement the setOnClickListener event on itemview and on click of item we display the name of the person with the help of Toast.<\/p>\n<pre> package abhiandroid.com.recyclerviewexample;\r\n import android.content.Context;\r\n import android.support.v7.widget.RecyclerView;\r\n import android.view.LayoutInflater;\r\n import android.view.View;\r\n import android.view.ViewGroup;\r\n import android.widget.ImageView;\r\n import android.widget.TextView;\r\n import android.widget.Toast;\r\n import java.util.ArrayList;\r\n public class CustomAdapter extends RecyclerView.Adapter {\r\n ArrayList personNames;\r\n ArrayList personImages;\r\n Context context;\r\n public CustomAdapter(Context context, ArrayList personNames, ArrayList personImages) {\r\n this.context = context;\r\n this.personNames = personNames;\r\n this.personImages = personImages;\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 holder.image.setImageResource(personImages.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 \/\/ init the item view's\r\n TextView name;\r\n ImageView image;\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 image = (ImageView) itemView.findViewById(R.id.image);\r\n }\r\n }\r\n }<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and pull down to refresh the content in RecyclerView.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android app Pull To Refresh aka SwipeRefreshLayout is used whenever we need to refresh the content&#8217;s of a view via a vertical swipe gesture. It accepts only one child means the component we want to refresh. It uses the listener mechanism to inform the listener who holds this component that a refresh event has &hellip; <a href=\"https:\/\/abhiandroid.com\/materialdesign\/pulltorefresh\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Pull To Refresh ListView &#038; RecyclerView Example In Android Studio &#8211; SwipeRefreshLayout<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-524","page","type-page","status-publish","hentry"],"psp_head":"<title>Pull To Refresh ListView &amp; RecyclerView Example In Android Studio - SwipeRefreshLayout \u2013 Android Material Design Tutorial<\/title>\r\n<meta name=\"description\" content=\"Learn how to use pull to refresh aka SwipeRefreshLayout in Android Studio with example using ListView and RecyclerView. Whenever we need to refresh the content&#039;s of a view via a vertical swipe gesture we use Pull To Refresh.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/materialdesign\/pulltorefresh\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages\/524","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages\/524\/revisions"}],"predecessor-version":[{"id":741,"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/pages\/524\/revisions\/741"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/materialdesign\/wp-json\/wp\/v2\/media?parent=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}