Saturday 20 June 2015

Android Swipe Down to Refresh ListView

SwipeRefreshLayout


You had seen in most of android apps like Gmail, Twitter  have an option to swipe / pull down to refresh it’s content. Whenever user swipes down from top, a loader will be shown and will disappear once the new content is loaded.

How it works

  • The ListView is placed as a child view to SwipeRefreshLayout.
  • This allows user the ability to show the loading spinner when user swipes the ListView edge. All the functionality of displaying loading bar is encapsulated inside SwipeRefreshLayout class.
  • When user swipes down, the OnRefreshListener events gets fired. You can handle this event to write the logic for downloading or refreshing data.
  • Note that, once data is downloaded, user has to manually call setRefreshing(false) to hide the refresh spinner.
Step 1) activity_main.xml

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"> 
 
 <!-- ListView is placed as a child view to SwipeRefreshLayout-->
 
     <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     </ListView> 
 
 </android.support.v4.widget.SwipeRefreshLayout>

Step 2) MainActivity

public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener{

private SwipeRefreshLayout mSwipeRefreshLayout;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
      // Configure the swipe refresh layout
        mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById 
                                (R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
     // change the color scheme 
        mSwipeRefreshLayout.setColorScheme(
                R.color.swipe_color_1, R.color.swipe_color_2,
                R.color.swipe_color_3, R.color.swipe_color_4);
 
 
        } 

// listener when swipe event occurs
     @Override
    public void onRefresh() {
        // Start showing the refresh animation
        mSwipeRefreshLayout.setRefreshing(true);

        //calling update listview method 
        updateListView();
          
    }
 
      private void updateListView() {

        // write the logic here to update listview
             new AsyncTask<Void, Void, Void>() {

   @Override
   protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
   }
   @Override
   protected Void doInBackground(Void... params) {
    //Write your long running process 
    return null;
   }
   
   @Override
   protected void onPostExecute(Void result) {
 
                         // Signify that we are done refreshing
                          mSwipeRefreshLayout.setRefreshing(false);
  
                        super.onPostExecute(result);

   }
  }.execute();

        
    } 

}