Swipe-to-refresh (or pull-to-refresh) is a way used to refresh information pulling the view down, like GMail and Twitter apps.


Bellow, I’ll show how to implement a ListView to use this method.

First, we need to extend the ListView.

The magic happens when onTouchEvent is overridden. See below:

	public boolean onTouchEvent(MotionEvent event) {
		//Actual Y position
		float y = event.getY();

		case MotionEvent.ACTION_DOWN:
			//Stores the Y position when screen is touched
			startY = y;
			//Allows refresh if the first visible item is the first in the list
			allowRefresh = (getFirstVisiblePosition() == 0);

		case MotionEvent.ACTION_MOVE:
				if((y - startY) > REFRESH_THRESHOLD){
					refresh = true;
					refresh = false;

		case MotionEvent.ACTION_UP:
				Toast.makeText(this.getContext(), "REFRESHING", Toast.LENGTH_SHORT).show();
			refresh = false;

		return super.onTouchEvent(event);

Basically, the method stores Y-axis position when screen is touched (MotionEvent.ACTION_DOWN) and, sliding down (MotionEvent.ACTION_MOVE), this position is compared  with initial Y-axis. If distance between initial position and end position is greater than REFRESH_THRESHOLD constant when MotionEvent.ACTION_UP occurs, the refresh command is triggered.

So far, only message “REFRESHING” will be displayed at refresh.

Implementing an interface can let this code more functional.

Create an interface with method onRefresh.

public void onRefresh();

Also, create a setter in our ListView. That will be your listener.

public void setRefreshListener(SwipeToRefreshListener listener){
	refreshListener = listener;

Change case MotionEvent.ACTION_UP to run implemented method.

//Executa a atualização
if(refresh &&
	refreshListener != null){
	//Chama o método que executará a atulização

In Activity we define the object that will run the update or, like the code below, shows the message “REFRESHING”.

swipeListView.setRefreshListener(new SwipeToRefreshListener() {
	public void onRefresh() {
		Toast.makeText(MainActivity.this, "REFRESHING", Toast.LENGTH_SHORT).show();

Now you can use this method to do anything you want, like importing data from text file, XML or database.

The complete example can be downloaded here.