跳转到相应内容

最常访问

最近访问

navigation

Responding to a Refresh Request

This lesson shows you how to update your app when the user requests a manual refresh, whether the user triggers the refresh with a swipe gesture or by using the action bar refresh action.

Respond to the Refresh Gesture

When the user makes a swipe gesture, the system displays the progress indicator and calls your app's callback method. Your callback method is responsible for actually updating the app's data.

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

You should put the code for the actual update operation in a separate method, and call that update method from your onRefresh() implementation. That way, you can use the same update method to perform the update when the user triggers a refresh from the action bar.

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.

For example, the following code implements onRefresh() and invokes the method myUpdateOperation() to update the data displayed by the ListView:

/*
 * 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();
        }
    }
);

Respond to the Refresh Action

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 the SwipeRefreshLayout 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 with setRefreshing(). The method which actually refreshes the data calls setRefreshing(false) to signal that the update is finished.

此网站会使用 Cookie 来存储您在此网站上指定的语言和显示选项偏好设置。

获取最新的 Android Developers 资讯和提示,助您在 Google Play 上取得成功。

* 必填字段

成功!

在微信上关注 Google Developers

要以浏览此网站吗?

您请求访问的是网页,但是您为此网站设置的语言偏好为

要更改您的语言偏好设置并以浏览此网站吗?如果以后您想要更改语言偏好设置,请使用每个页面底部的语言菜单。

该类需要 或更高的 API 级别

此文档已被隐藏,因为您为该文档选择的 API 级别是 。您可以使用左侧导航栏上方的选择器来更改文档的 API 级别。

要详细了解如何根据您的应用需求指定 API 级别,请参阅支持不同平台版本

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)