android-Supporting Swipe-to-Refresh,Adding Swipe-to-Refresh To Your App,
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 GridView
child.
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 ListView
with the ID "@android:id/list"
,
the swipe-to-refresh functionality is automatically supported.
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.OnRefreshListener
interface
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.
相關文章
- [譯] Architecture Components 之 Adding Components to your ProjectProject
- Running the app on your deviceAPPdev
- Writing your first Django app, part 1DjangoAPP
- Circular Reference with Mongo Appender can Crash Your ProcessGoAPP
- redhat your system appears to have shut down uncleanlyRedhatAPP
- Adding a Wordfile in Ultraedit
- Using SQLite in your Windows 8 Metro style applicationsSQLiteWindowsAPP
- AC040--Mapping Your Enterprise in COAPP
- Your Prediction Gets As Good As Your DataGo
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.APPContextPackage
- No suitable application records were found. Verify your bundle ideUIAPPIDE
- Your PHP installation appears to be missing the MySQL extension which is requirePHPAPPMySqlUI
- Bootstrapping Your Startup idea - 如何低成本創業bootAPPIdea創業
- Setting up your App domain for SharePoint 2013APPAI
- Your title
- android-Making Your App Content Searchable by Google,Specifying App Content for IndexingAndroidAPPGoIndex
- android-Purchasing In-app Billing Products,Testing Your In-app Billing ApplicationAndroidAPP
- Prettier your projectProject
- Adding Drivers into VMWare ESXi Installation Image
- 【build your own xxx】實現你自己的call和applyUIAPP
- 12c-Say goodbye to your backup when dropping your PDBGo
- android-Preparing Your In-app Billing Application,Establishing In-app Billing Products for SaleAndroidAPP
- Adding List Item Element At Runtime In Oracle FormsOracleORM
- Python internals: adding a new statement to PythonPython
- In your opinion with 6 sentences
- deploy-your-site
- translate-your-site
- talk-to-your-data
- WWDC 2017:Your Apps and Evolving Network Security StandardsAPP
- 解決問題-There appears to be trouble with your network connection. Retrying...APP
- Scan Your Truck Using Nexiq Adapter: Simplifying Your Diagnostic ProcessAPT
- Generating a new SSH key and adding it to the SSH-agent
- Creating a Password File and Adding New Users to It (47)
- How Good Are Your Opinion 2Go
- Speed Up Your WordPress Site
- Bugs Fixed for Your PlatformPlatform
- Backup your Linux SystemLinux
- connect your tunnel to CloudflareCloud