TrinityTuts

Android new SwipeRefresh Layout

Last updated on July 31st, 2016 at 11:14 am

Android swipe refresh is very useful to refresh list if your app not auto synchronize data from web service in this post i explain how to implement new SwipeRefresh layout. If you want to read about old SwipeRefresh layout you follow my last post here.

SwipeRefresh is very easy to implement in your project.

Code

Step 1. Create new project in Android Studio / Eclipse.

Step 2. You need to add appcompat support library in your project to make this thing work.

Add appcompat in your build.gradle

compile 'com.android.support:appcompat-v7:21.0.3'

Step 3. Now open your activity file and add swiperefresh layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--Add your list view here-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Swipe down to refresh" />

    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

Step 4. Now you need to handle swipe when user make over list

package com.swiperefresh;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;


public class MainActivity extends Activity {

    private SwipeRefreshLayout mSwipeRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Load new data from service or do some thing here :-)
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 2500);
            }
        });
    }
}

Now you load your data and add it into your listview inside onrefresh() method

@Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Load new data from service or do some thing here :-)
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 2500);
            }