Android SwipeRefreshLayout教程
SwipeRefreshLayout也是一種下拉刷新控件,不同的它的刷新狀態效果和傳統的PuulToRefresh完全不一樣,具體效果可以參考Google Now的刷新效果,見下圖:

SwipeRefreshLayout已經被放到了sdk中,在version 19.1之后SwipeRefreshLayout 被放到support v4中。
SwipeRefreshLayout控件值允許有一個子元素:我們想滑動刷新的對象。它使用Listener機制來告之持有SwipeRefreshLayout的組建某個事件發生了,也就是說假如是activity持有SwipeRefreshLayout,那么activity就必須實現一個接口來接收通知,這個接口中需要實現的主要是onRefresh()方法。
除此之外SwipeRefreshLayout還提供了一些公共方法:
setOnRefreshListener(OnRefreshListener): 為布局添加一個Listener
setRefreshing(boolean): 顯示或隱藏刷新進度條
isRefreshing(): 檢查是否處于刷新狀態
setColorScheme(): 設置進度條的顏色主題,最多能設置四種
如何使用
我將通過一個產生隨機數字的demo來講解如何使用SwipeRefreshLayout,刷新一次就產生隨機數字。
![android_swipetorefreshlayout[5]](https://simg.open-open.com/show/6206e69e2a8b2e7daf96186fd5b554aa.png)
一般SwipeRefreshLayout作為根節點被放入布局文件中:
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/swipe">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random number:"
android:id="@+id/lbl"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rndNum"
android:layout_toRightOf="@id/lbl"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lbl"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Swipe to Refresh"
style="@android:style/TextAppearance.Medium"/>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
上面的代碼中SwipeRefreshLayout只有一個為scrollView的子元素。接下來activity中:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe);
final TextView rndNum = (TextView) findViewById(R.id.rndNum);
swipeView.setColorScheme(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
Log.d("Swipe", "Refreshing Number");
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
double f = Math.random();
rndNum.setText(String.valueOf(f));
}
}, 3000);
}
});
}
.... 所有的代碼都在onCreate中了。
上面的代碼很簡單,只需要給SwipeRefreshLayout添加一個listener,值得說明的是setColorScheme方法是設置刷新進度條的顏色,最多只能設置4種循環顯示,默認第一個是隨用戶手勢加載的顏色進度條。
源碼
github上有SwipeRefreshLayout的例子源碼,地址在:SwipeRefreshLayoutDemo