Download Code

In this tutorial you will learn about Swipe Refresh Layout ListView.How to Drag down vertically to load data with Swipe Refresh Layout.You should learn basic of ListView and Runnable and Thread Handler.

Swipe Refresh Layout

The Swipe Refresh Layout used whenever the user want to refresh the contents of a view via a vertical swipe gesture.Official Documentation Link.

Create new android application project.Go to activity_main.xml and add Swipe Refresh layout ListView.

Swipe Refresh Layout ListView

activity_main.xml

swipe refresh layout listview,listview,swiperefreshlayout,Swipe Refresh Layout,Swipe Refresh layout in android tutorial, swiperefreshlayout android source,onrefreshListener in android, swipe refresh layout onrefreshlistener,how to drag down to load data in android, how to drag a listview to load data android source code, handler,runnable,run,ArrayAdapter,setAdapter,runnable and thread handler, thread handler in android,thread handler,

Next go to values directory and create XML file “color.xml”.You are going to design your progress circle.

color.xml

swipe refresh layout listview,listview,swiperefreshlayout,Swipe Refresh Layout,Swipe Refresh layout in android tutorial, swiperefreshlayout android source,onrefreshListener in android, swipe refresh layout onrefreshlistener,how to drag down to load data in android, how to drag a listview to load data android source code, handler,runnable,run,ArrayAdapter,setAdapter,runnable and thread handler, thread handler in android,thread handler,

At last go to MainActivity.java and do the following steps.

  • Create Swipe Refresh Layout and ListView variables.
  • Refer id’s of Swipe layout and listview.
  • Create string array which will load when you drag down the layout.
  • Create ArrayAdapter of String,it will use to set array to listView.
  • Apply onRefreshListener method on swipe layout by implementing interface.
  • Apply color on swipe progress circle using setColorScheme method.
  • Within onRefresh method use runnable to set list adapter.

MainActivity.java

package developer.simpleswiperefresh;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements OnRefreshListener  {
	private SwipeRefreshLayout swipeLayout;
	ListView codecList;   // listview variable
	// string array for list menu
	String androidCodec[] = {"Java","C#","C++","Php","Javascript","Android","Codec"};
	ArrayAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		codecList = (ListView) findViewById(R.id.list);   // refering listview of xml
		//setting array adapter with item layout
    	adapter = new ArrayAdapter(this, R.layout.item,R.id.itemTxt, androidCodec);
		swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		swipeLayout.setOnRefreshListener(this);
		swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
				android.R.color.holo_green_light, 
				android.R.color.holo_orange_light, 
				android.R.color.holo_red_light);
	}


	@Override 
	public void onRefresh() {
		new Handler().postDelayed(new Runnable() {
			@Override public void run() {
				swipeLayout.setRefreshing(false);
				 
		        codecList.setAdapter(adapter);   // setting adapter to listview
			}
		}, 5000);
	}
}

Run your android application project.Swipe/Drag down the layout.

swipe refresh layout listview,listview,swiperefreshlayout,Swipe Refresh Layout,Swipe Refresh layout in android tutorial, swiperefreshlayout android source,onrefreshListener in android, swipe refresh layout onrefreshlistener,how to drag down to load data in android, how to drag a listview to load data android source code, handler,runnable,run,ArrayAdapter,setAdapter,runnable and thread handler, thread handler in android,thread handler,
That’s it for this tutorial.
Thanks.