Wednesday, 21 February 2018

Learn How To Implement Swipe To Refresh in Android

In this tutorial we'll learn how to implement SwipeRefreshLayout. Here we'll create a ListView and then implement Swipe To Refresh on that.

Below is the widget code for SwipeRefreshLayout
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe">
</android.support.v4.widget.SwipeRefreshLayout>

Create a new Project in Android Studio and select EmptyActivity.

1. Open main_activity.xml and do the following changes
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe"
    tools:context="com.minixithub.swipetorefreshtutorial.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:nestedScrollingEnabled="true"
            android:layout_height="match_parent">
</ListView>
    </LinearLayout>

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

2.Open MainActivity.java class and Implement the following code
package com.minixithub.swipetorefreshtutorial;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity 
    implements SwipeRefreshLayout.OnRefreshListener{

    private ListView listView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ArrayList<String> arrayList;
    private Context context;
    private ArrayAdapter<String>adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        arrayList = new ArrayList<>();
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe);
        listView = (ListView)findViewById(R.id.list);
        setRecordToListView();
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private void setRecordToListView()
    {
        for(int i = 0;i<10;i++){
            addNewRecord();
        }
        adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(adapter);

    }
//    Function to Add new Record
    private void addNewRecord(){
        int size = arrayList.size();
        String record = "Record No "+size+1;
        arrayList.add(0,record);
    }

    @Override
    public void onRefresh() {
//        Displaying Loader 
        swipeRefreshLayout.setRefreshing(true);
//        Creating Handler
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                addNewRecord();
//                Notifing The ListView
                adapter.notifyDataSetChanged();
                listView.deferNotifyDataSetChanged();
            }
        },2000);
//        Hiding the Loader
        swipeRefreshLayout.setRefreshing(false);
    }
}

No comments:

Post a Comment