Pull to refresh

Probably everyone knows pull to refresh UI pattern on android. You can check it in the Google’s Gmail application.

Today I wanted to implement similar behavior but instead of pulling from the top, I wanted to pull from the bottom.

First I tried Google’s Swipe Refresh Layout. It’s part of Support Library v4 - 19.1.0. You can find it in:

/sdk/extras/android/m2repository/com/android/support/support-v4/19.1.0

Library is simple to use but supports only one child as view. If you want to use it, simply create layout like this:

1
2
3
4
5
6
7
8
9
10
11
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

      <ListView
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"/>

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

then in java set colors and implement SwipeRefreshLayout.OnRefreshListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MyActivity extends ActionBarActivity
              implements SwipeRefreshLayout.OnRefreshListener {

  @Override
  public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.main);

	  swipeRefreshLayout.setOnRefreshListener(this);
	  swipeRefreshLayout.setColorScheme(
          android.R.color.holo_blue_bright,
	        android.R.color.holo_blue_dark,
	        android.R.color.holo_green_dark,
	        android.R.color.holo_purple);
    }

    @Override
    public void onRefresh() {
	    //do your work here, call this method in the end:
	    //swipeLayout.setRefreshing(false);
    }
}