Using Swipe-to-refresh in your Android application - Tutorial
version 0.1, 18.06.2016
This tutorial contains information how to implement the swipe to refresh design pattern.
1. Supporting swipe to refresh
Android provides a widget that implements the swipe-to-refresh design pattern that allows the user to trigger an update with a vertical swipe.
This is implemented by the SwipeRefreshLayout widget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback methods in your app.
To support this, add the widget to your layout file as the parent of the relevant view, and implementing the refresh behavior that gets invoked when the user swipes.
To use the swipe to refresh widget ensure you have a dependency to the support library in your application Gradle build file.
dependencies {
compile 'com.android.support:support-v4:25.3.1'
}
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
In the SwipeRefreshLayout.OnRefreshListener method you implement the logic to update your view.
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
doYourUpdate();
}
}
);
private void doYourUpdate() {
// TODO implement a refresh
setRefreshing(false); // Disables the refresh icon
}
2. Swipe to refresh links
2.1. vogella Java example code
If you need more assistance we offer Online Training and Onsite training as well as consulting
