Skip to main content

How to use SwipeRefreshLayout? Example

Google just released a new version of Android Support Library with SwipeRefreshLayout[1]. It is pull to refresh layout based on Google Now, not Gmail. Before this release most of android developers use PullToRefresh library from Chris Banes (Special thanks to him for great libraries and android development tips) But with the recent release of Google, Chris drop his support to own library - PullToRefresh. [2] We have SwipeRefreshLayout now it is simple to use with a few line of codes.


Step One: Install


Update your Android Support Library in Android SDK manager and make sure you have version 19.1. Then right click on project and select "Android Tools->Add Support Library". After instaltion your project has brand new support library.


Step Two: Layout


You should add your scrool view or list view in to a "SwipeRefreshLayout"


<android.support.v4.widget.SwipeRefreshLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/srl_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>
           <ListView
               android:id="@+id/listView1"
               android:layout_width="match_parent"
               android:layout_height="wrap_content">
           </ListView>
</android.support.v4.widget.SwipeRefreshLayout>


Step Three: Code


In code we need a SwipeRefreshLibrary instance.


private SwipeRefreshLayout mSwipeRefreshLayout;
...

// onCreate()
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_layout);

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
mSwipeRefreshLayout.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);
...
// onRefresh() method when refresh finished.
  @Override public void onRefresh() {
      // Empty implementation
  }


Now you are ready try this in your app. I just use it my new app, please try and do not hesitate to send your feedback :)



Comments