android-Supporting Swipe-to-Refresh,Adding Swipe-to-Refresh To Your App,

desaco發表於2016-01-31

Even if your app automatically updates its content on a regular basis, you can allow users to request manual updates as well. For example, a weather forecasting app can allow users get the latest forecasts on demand. To provide a standard user experience for requesting updates, the Android platform includes the swipe-to-refresh design pattern, which allows users to trigger an update with a vertical swipe.

Note: This class requires the latest version of the Android v4 Support Library APIs. If you have not used the Support Library before, follow the instructions in the Support Library Setup document.

>Adding Swipe-to-Refresh To Your App

You enable this behavior by adding the widget to your layout file as the parent of a ListView orGridView, and implementing the refresh behavior that gets invoked when the user swipes.

> To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a singleListView or GridView. Remember that SwipeRefreshLayout only supports a single ListView or GridViewchild.

The following example demonstrates how to add the SwipeRefreshLayout widget to an existing layout file containing a ListView:

<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>

You can also use the SwipeRefreshLayout widget with a ListFragment. If the layout contains a ListViewwith the ID "@android:id/list", the swipe-to-refresh functionality is automatically supported. 

 You should add a refresh action to your app's action bar to ensure that users who may not be able to perform a swipe gesture can still trigger a manual update. 

 You should add the refresh action as a menu item, rather than as a button, by setting the attributeandroid:showAsAction=never. If you display the action as a button, users may assume that the refresh button action is different from the swipe-to-refresh action. By making the refresh action less conspicuous in the action bar, you can encourage users to perform manual updates with the swipe gesture while still maintaining the accessible option in a place where D-pad users would look for it.

The following code demonstrates how to add the swipe-to-refresh action to the overflow area:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_refresh"
        android:showAsAction="never"
        android:title="@string/menu_refresh"/>
</menu>
Responding to a Refresh Request

 To respond to the refresh gesture in your app, implement the SwipeRefreshLayout.OnRefreshListenerinterface and its onRefresh() method. The onRefresh() method is invoked when the user performs a swipe gesture.

 Your update method calls setRefreshing(false) when it has finished updating the data. Calling this method instructs the SwipeRefreshLayout to remove the progress indicator and update the view contents.

/*
 * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user
 * performs a swipe-to-refresh gesture.
 */
mySwipeRefreshLayout.setOnRefreshListener(
    new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");

            // This method performs the actual data-refresh operation.
            // The method calls setRefreshing(false) when it's finished.
            myUpdateOperation();
        }
    }
);
>If the user requests a refresh by using the action bar, the system calls the onOptionsItemSelected() method. Your app should respond to this call by displaying the progress indicator and refreshing the app's data.

 To respond to the refresh action, override onOptionsItemSelected(). In your override method, trigger theSwipeRefreshLayout progress indicator by calling setRefreshing() with the value true, then perform the update operation. Once again, you should be doing the actual update in a separate method, so the same method can be called whether the user triggers the update with a swipe or by using the action bar. When the update has finished, call setRefreshing(false) to remove the refresh progress indicator.

 The following code shows how to respond to the request action:

/*
 * Listen for option item selections so that we receive a notification
 * when the user requests a refresh by selecting the refresh action bar item.
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        // Check if user triggered a refresh:
        case R.id.menu_refresh:
            Log.i(LOG_TAG, "Refresh menu item selected");

            // Signal SwipeRefreshLayout to start the progress indicator
            mySwipeRefreshLayout.setRefreshing(true);

            // Start the refresh background task.
            // This method calls setRefreshing(false) when it's finished.
            myUpdateOperation();

            return true;
    }

    // User didn't trigger a refresh, let the superclass handle this action
    return super.onOptionsItemSelected(item);

}
 Note: When the user triggers a refresh with a swipe action as described in Respond to the Refresh Gesture, you do not need to call setRefreshing(). The SwipeRefreshLayout widget takes care of displaying the progress indicator and removing it when the update has finished. However, if the update is triggered by any means other than a swipe gesture, you need to explicitly turn the progress indicator on withsetRefreshing(). The method which actually refreshes the data calls setRefreshing(false) to signal that the update is finished.

相關文章