Quick watch on SwipeRefreshLayout (Tutorial)

7:02 PM 1 Comments


After getting update in android support v4, I thought lets check it out. And believe me it is very light and very simple to implement. First of all update Android Support Library from SDK manager. The SwipeRefreshLayout works with Vertical swipe gesture. Lets go through tutorial. Please dont go through coding standards because this is not deep dive ;). 




Create xml with SwipeRefreshLayout.

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

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

Create MainActivity.java

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 /**
  * A place holder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  private SwipeRefreshLayout layout;
  String[] values = new String[] { "Android", "iPhone", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
  ArrayList<String> list = new ArrayList<String>();

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   
   layout = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_main, container, false);
   final ListView listView = (ListView) layout.findViewById(R.id.listview);
   
   for (int i = 0; i < values.length; i++) {
    list.add(values[i]);
   }
   final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
   listView.setAdapter(adapter);
   
   layout.setColorScheme(android.R.color.holo_green_dark, android.R.color.holo_orange_dark, android.R.color.holo_blue_dark,
     android.R.color.holo_red_dark);
   layout.setOnRefreshListener(new OnRefreshListener() {

    @Override
    public void onRefresh() {
     Log.e(getClass().getSimpleName(), "refresh");
     for (int i = 0; i < values.length; i++) {
      list.add(values[i]);
     }
     adapter.notifyDataSetInvalidated();
     new GetLinks().execute();
    }
   });
   return layout;
  }

  public class GetLinks extends AsyncTask<Void, Void, Void> {

   @Override
   protected void onPreExecute() {
    super.onPreExecute();

   }

   @Override
   protected Void doInBackground(Void... params) {
    try {
     Thread.sleep(5000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    return null;
   }

   @Override
   protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    layout.setRefreshing(false);
   }
  }

 }

}

Note: This component should be parent view and support only one child.

SourceCode : SwipeRefreshLayout

 

 Reference : SwipeRefreshLayout on devloper site


Karn Shah

My name is Karn Shah, I am a software developer living in Ahmedabad,India and my job/hobby is Android. I have been working on the Android platform since 2011 and I hope that sharing what I have learned will help people get proficient in Android faster!

1 comments: