Anyway, here's a short demo of using this nifty little view.
|
|
|
The app is a simple ListView that shows a list of countries. Swiping down on the list will simulate a long-running activity (like connecting to a server, for example) and afterwards updating the list with new data.
Adding support for swipe to refresh is pretty straightforward. You would have to wrap the swipe-able layout in a
SwipeRefreshLayout
. Here is my XML file:<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
In your
Fragment
, you would then have to put an onRefreshListener
on this SwipeRefreshLayout
. In my Fragment
's onCreateView
, I have this:// Configure the swipe refresh layout
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.container);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorScheme(
R.color.swipe_color_1, R.color.swipe_color_2,
R.color.swipe_color_3, R.color.swipe_color_4);
The colors are defined in a
colors.xml
file in my res/values
folder. To show or hide the refresh animation, you would have to call setRefreshing(boolean)
. This means that if you have to kick off an AsyncTask
when the user swipes down, call setRefreshing(true)
in onPreExecute
, and call setRefreshing(false)
in onPostExecute
.The implementation of
onRefresh
in the demo app is pretty simple, it simply grabs the next bunch of countries from a pre-defined list.@Override
public void onRefresh() {
// Start showing the refresh animation
mSwipeRefreshLayout.setRefreshing(true);
// Simulate a long running activity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
updateCountries();
}
}, 5000);
}
That's it really. Nice and short. The code for this demo is in Github.