SwipeRefreshLayout exists as a support library and enables implement the now popular Swipe to Refresh.Some call it Pull To Refresh,some drag to refresh but the concept is simple,pull or swipe down on your layout and this causes display of a small progress circle.We can then of course capitalize on that and refresh our dataset.

What we do :

  • Custom RecyclerView with cardviews with images and text.
  • Our data source is a simple arraylist.
  • Use SwipeRefreshlayout.
  • When user swipes down,we simulate downloading of fresh copies of data.
  • We do this in background thread using handlers.
  • Rebind the data to our recyclerview.


SECTION 1 : Our Dependencies

  • Android Studio has added for us dependencies for AppCompat and Design support libraries.
  • Note we are using AppCompatActivity.
  • Design support libraries are for cordinatorlayout,appbar layout,toolbar and floating action button.All contained in our ActivityMain.xml layout specification.Also swiperefreshlayout.
  • Our RecyclerView’s ViewItems shall be cardviews.So add CardView also.
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.tutorials.hp.swiperefreshrecyclerview"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:cardview-v7:23.2.1'


SECTION 2 : Our Data Object


Main Responsibility : IS A SINGLE DATA OBJECT

  • Represents a single object.Here I’ll use Movie.
  • The object shall have various properties like name,image etc.
  • Each object shall occupy a specific viewitem, here cardview.
package com.tutorials.hp.swiperefreshrecyclerview;

public class Movie {
    private String name;
    private int image;

    public Movie(String name, int image) {
        this.name = name;
        this.image = image;

    public String getName() {
        return name;

    public int getImage() {
        return image;

SECTION 3 : ReyclerView classes

ItemClickListener inetrface


  • Define our RecyclerView ViewItem’s signature for our OnItemClick methods.
package com.tutorials.hp.swiperefreshrecyclerview;

public interface ItemClickListener {

    void onItemClick(int pos);


View Holder class

Main Responsibility : HOLD VIEWS FOR RECYCLING.

  • In this case textviews and imageviews.
  • Subclasses RecyclerView.ViewHolder.
package com.tutorials.hp.swiperefreshrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    ImageView img;
    TextView nameTxt;
    ItemClickListener itemClickListener;

    public MyHolder(View itemView) {

        img= (ImageView) itemView.findViewById(R.id.movieImage);
        nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);


    public void setItemClickListener(ItemClickListener ic)

    public void onClick(View v) {

Adapter class


  • Subclasses RecyclerView.Adapter
  • Binds our dataset to our views.
  • Shall receive an arraylist and a context.
  • We shall simulate fetching updates.
  • We do this in background thread using Handlers.
  • Manwhile we show a progress circle.
package com.tutorials.hp.swiperefreshrecyclerview;

import android.content.Context;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

    Context c;
    ArrayList<Movie> movies;
    SwipeRefreshLayout swiper;

    public MyAdapter(Context c, ArrayList<Movie> movies, SwipeRefreshLayout swiper) {
        this.c = c;
        this.movies = movies;
        this.swiper = swiper;

    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,parent,false);
        MyHolder holder=new MyHolder(v);

        return holder;

    public void onBindViewHolder(MyHolder holder, int position) {

        holder.setItemClickListener(new ItemClickListener() {
            public void onItemClick(int pos) {
                Toast.makeText(c, movies.get(pos).getName(), Toast.LENGTH_SHORT).show();

        swiper.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            public void onRefresh() {

    public int getItemCount() {
        return movies.size();

    private void refresh()
        new Handler().postDelayed(new Runnable() {
            public void run() {
                movies.add(0,movies.get(new Random().nextInt(movies.size())));



SECTION 4 : Our Activity

MainActivity class.

Main Responsibility : LAUNCH OUR APP.

  • We shall reference the views like RecyclerView and Floating action button here,from our XML Layouts.Also SwipeRefreshLayout.
  • We then generate our dataset and pass it to an instance of our adapter.
  • We set the adapter to our RecyclerView.

SECTION 5 : Our Layouts

ActivityMain.xml Layout.

  • Inflated as our activity’s view.
  • Contains xml specifications for Cordinator Layout,appbar layout,toolbar as well as floating action button.All these are contained in Design Support library.
  • Includes content main where we define our view hierarchy.

ContentMain.xml Layout.

  • Defines our view hierarchy.
  • Wraps our AdapterView with SwipeRefreshLayout.

Model.xml Layout.

  • Inflated as our AdapterView’s viewitems.
  • How our CardViews shall look.


Lets share more tips in OUR FB PAGE. Cheers.