Life of Apps

Implementing Pull to Refresh

Implementing the "pull to refresh" feature in Android 5.1 upwards is fairly straightforward. Here are the steps:

1. Wrap the list where you want to implement it with a SwipeRefreshLayout

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

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

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

2. Get the SwipeRefreshLayout in the Activity or Fragment in onCreateView method like so

swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);

3. If using a Fragment, add a method onViewCreated. In it attach the onRefreshListener to the SwipeRefreshLayout

 @Override
 public void onViewCreated(View view, Bundle savedInstanceState)
 {
  super.onViewCreated(view, savedInstanceState);
  swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
   @Override
   public void onRefresh() {
    Log.i(TAG, "onRefresh called from SwipeRefreshLayout");

    //add code here to refresh the list
   }
  });
 }

Done. 

Danesh

Visit Pleb.in for apps developed by Danesh

No comments :

Post a Comment

Leave a Comment...