前言:
從今天開始,學習和記錄一個學習google自釋出MaterialDesign風格而加入的新控件.今天是看的SwipeRefreshLayout,一個下拉重新整理控件.現在很多app上都用的是這個,網易,知乎等都用上了,MaterialDesign風格标準化真的是大勢所趨.先看一下網易的效果:

需求分析:
今天主要是想實作一下幾個功能
1.手勢下拉進入重新整理狀态,出現重新整理的圓形箭頭,開始重新整理資料,資料重新整理結束後隐藏
2.進入頁面的時候,自動重新整理資料,重新整理結束後隐藏:
開始使用:
1.布局上聲明使用:
這裡要注意的地方是swipeRefreshLayout是一個特殊的布局,隻允許有一個直接子控件.
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
2.手動下拉重新整理功能實作:
activity綁定資料,設定重新整理監聽:
在onRefresh()裡面做相應重新整理資料的操作,我這裡為了測試,就隻是做的空倒計時後關閉重新整理
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {//正在重新整理
new CountDownTimer(3000, 500) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
//重新整理結束
swipeRefreshLayout.setRefreshing(false);
}
}.start();
}
});
3.自動下拉重新整理功能實作:
思路是剛進入頁面的時候,讓swipeRefreshLayout.setRefreshing(true),然後走OnRefreshListener監聽事件的onRefresh()方法加載重新整理資料,一切都是那麼剛剛好.
然而試了之後并沒有什麼用,原因在于swipeRefreshLayout.setRefreshing(true)方法不會觸發OnRefreshListener.
解決方法:手動調用OnRefreshListener中的onRefresh()方法
(1)定義OnRefreshListener監聽事件,為全局變量
SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new CountDownTimer(3000, 500) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
//重新整理結束
swipeRefreshLayout.setRefreshing(false);
}
}.start();
}
};
(2)根據我們的需要,在onCreate()或者onResum()中手動使swipeRefreshLayout處于重新整理狀态,并調用OnRefreshListener中的onRefresh()方法
//設定為正在重新整理狀态
swipeRefreshLayout.setRefreshing(true);
//觸發監聽的onRefresh()方法
onRefreshListener.onRefresh();
更多了解:
其實想知道更多關于這個控件的使用,最好的方式就是看源碼,而且這個控件的源碼才1200多行,看起來不會有太大壓力,注釋寫的都很清楚,例如這個類注釋:
/**
* The SwipeRefreshLayout should be used whenever the user can refresh the
* contents of a view via a vertical swipe gesture. The activity that
* instantiates this view should add an OnRefreshListener to be notified
* whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout
* will notify the listener each and every time the gesture is completed again;
* the listener is responsible for correctly determining when to actually
* initiate a refresh of its content. If the listener determines there should
* not be a refresh, it must call setRefreshing(false) to cancel any visual
* indication of a refresh. If an activity wishes to show just the progress
* animation, it should call setRefreshing(true). To disable the gesture and
* progress animation, call setEnabled(false) on the view.
* <p>
* This layout should be made the parent of the view that will be refreshed as a
* result of the gesture and can only support one direct child. This view will
* also be made the target of the gesture and will be forced to match both the
* width and the height supplied in this layout. The SwipeRefreshLayout does not
* provide accessibility events; instead, a menu item must be provided to allow
* refresh of the content wherever this gesture is used.
* </p>
*/
說明了swipeRefreshLayout的主要用途和使用注意事項,總結一下主要是這幾個點:
1.使用OnRefreshListener監聽重新整理
2.使用setRefreshing(boolean isRegreshing)設定重新整理狀态
3.使用setEnable(boolean isEnable)設定是否禁用手勢下拉重新整理
4.隻支援一個直接子View,布局要求設定為寬和高都鋪滿.
還有很多其他的功能,例如設定swipeRefreshLayout的樣式等,都可以從源碼中找到,從structure視窗檢視超級友善,今天太晚了,明天有時間會都測試一下.