下拉刷新控件(4)SwipeRefreshLayout官方教程(上)如何在应用中使用它
http://developer.android.com/training/swipe/add-swipe-interface.html
The swipe-to-refresh user interface pattern is implemented entirely within the SwipeRefreshLayout
widget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback methods in your app. You enable this behavior by adding the widget to your layout file as the parent of a ListView
or GridView
, and implementing the refresh behavior that gets invoked when the user swipes.
This lesson shows you how to add the widget to an existing layout. It also shows you how to add a refresh action to the action bar overflow area, so that users who may be unable to use the swipe gesture can trigger a manual update with an external device.
Add the SwipeRefreshLayout Widget
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
orGridView
child.
The following example demonstrates how to add the SwipeRefreshLayout
widget to an existing layout file containing a ListView
:
1 <android.support.v4.widget.SwipeRefreshLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/swiperefresh" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <ListView 8 android:id="@android:id/list" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" /> 11 12 </android.support.v4.widget.SwipeRefreshLayout>
You can also use the SwipeRefreshLayout
widget with a ListFragment
. If the layout contains aListView
with the ID "@android:id/list"
, the swipe-to-refresh functionality is automatically supported. However, explicitly declaring the ListView
in this way supersedes the default ListFragment
view structure. If you want to use the default view structure, you will have to override parts of the SwipeRefreshLayout
andListFragment
behavior. For an example of how to do this, see the SwipeRefreshListFragment sample app.
Add a Refresh Action to the Action Bar
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. For example, users with accessibility issues can trigger action bar actions using external devices, such as keyboards and D-pads.
You should add the refresh action as a menu item, rather than as a button, by setting the attribute android: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:
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" > 2 <item 3 android:id="@+id/menu_refresh" 4 android:showAsAction="never" 5 android:title="@string/menu_refresh"/> 6 </menu>
【推荐】国内首个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,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?