先看下效果图
本控件有四个主要的方法
1.setOnRefreshListener(OnRefreshListener listener);
设置一个监听器,当开始刷新的时候调用监听器的onRefresh方法
2.setRefreshing(boolean)
设置进度条是否可见
3.isRefreshing()
返回刷新状态
4.setColorScheme()
设置进度条颜色,最多可以设置4种变换
使用方法:
SwipeRefreshLayout的用法也很简单,首先在xml文件里面定义,注意,这里SwipeRefreshLayout跟ScrollView一样,只能有一个子控件,所以我们可以放ListView,GridView或者ScrollView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
在Java代码中
swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);
swipeLayout.setOnRefreshListener(this);
// 顶部刷新的样式
swipeLayout.setColorScheme(android.R.color.holo_red_light,
android.R.color.holo_green_light,
android.R.color.holo_blue_bright,
android.R.color.holo_orange_light);
//当你要停止刷新动画的时候
swipeLayout.setRefreshing(false);就行了