使用SwipeRefreshLayout可以实现下拉刷新,前提是布局里需要包裹一个可以滑动的子控件,然后在代码里设置OnRefreshListener设置监听,最后在监听里设置刷新时的数据获取就可以了。由于是新出来的东西,所以要想使用,先把support library的版本升级到19.1或更新。
使用方法非常简单,只需要再布局中引入这个控件就可以了
android.support.v4.widget.SwipeRefreshLayout
此控件位于support.v4.widget中,代码中直接饮用即可,一般都是作为根节点放入布局中
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/rndNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="SwipeRefreshLayout"/> </RelativeLayout> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout>
在MainActivity中findview就可以使用SwipeRefreshLayout
setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener
setRefreshing(boolean): 显示或隐藏刷新进度条
isRefreshing(): 检查是否处于刷新状态
setColorScheme(): 设置进度条的颜色主题(可设置多个颜色,转一次换一个颜色)