下拉刷新控件(5)SwipeRefreshLayout官方教程(下)响应刷新事件
http://developer.android.com/training/swipe/respond-refresh-request.html
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
:
1 /* 2 * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user 3 * performs a swipe-to-refresh gesture. 4 */ 5 mySwipeRefreshLayout.setOnRefreshListener( 6 new SwipeRefreshLayout.OnRefreshListener() { 7 @Override 8 public void onRefresh() { 9 Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout"); 10 11 // This method performs the actual data-refresh operation. 12 // The method calls setRefreshing(false) when it's finished. 13 myUpdateOperation(); 14 } 15 } 16 );
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 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:
1 /* 2 * Listen for option item selections so that we receive a notification 3 * when the user requests a refresh by selecting the refresh action bar item. 4 */ 5 @Override 6 public boolean onOptionsItemSelected(MenuItem item) { 7 switch (item.getItemId()) { 8 9 // Check if user triggered a refresh: 10 case R.id.menu_refresh: 11 Log.i(LOG_TAG, "Refresh menu item selected"); 12 13 // Signal SwipeRefreshLayout to start the progress indicator 14 mySwipeRefreshLayout.setRefreshing(true); 15 16 // Start the refresh background task. 17 // This method calls setRefreshing(false) when it's finished. 18 myUpdateOperation(); 19 20 return true; 21 } 22 23 // User didn't trigger a refresh, let the superclass handle this action 24 return super.onOptionsItemSelected(item); 25 26 }
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?