In this post I will describe how to use SwipeRefreshLayout which is part from support library too.
Using this new layout is pretty easy and this is a standard way to implement common Pull to refresh pattern.
We must know: SwipeRefreshLayout is a ViewGroup layout which can hold only one scrollable view as children.
Example layout file with Swipe Refresh
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
..............................................
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
We can set OnRefreshListener to listen when user wants to refresh content
mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
mSwipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
System.out.println("Updated");
mSwipeLayout.setRefreshing(false);
}
});
If we want we can use it to display refreshing content before user iteract. For this we can use setRefreshing method
mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
mSwipeLayout.post(new Runnable() {
@Override
public void run() {
mSwipeLayout.setRefreshing(true);
}
});