SwipeRefreshLayout(下拉刷新的佈局)繼承自ViewGroup,在support v4兼容包下,但必須把你的support library的版本升級到19.1。
SwipeRefleshLayout常用的方法:
setOnRefreshListener(OnRefreshListener): 爲佈局添加一個刷新數據的Listener
setRefreshing(boolean): 顯示或隱藏刷新進度條
isRefreshing(): 檢查是否處於刷新狀態
setColorScheme(): 設置進度條的顏色主題,最多能設置四種,這個方法已經作廢,目前改爲用setColorSchemeResources方法來設置了,參數是一樣的。
下面實現一個Demo:
xml佈局文件:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.activitydemo.MainActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="@string/hello_world" />
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Activity代碼實現:
package com.example.activitydemo;
import android.app.Activity;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
public class MainActivity extends Activity implements OnRefreshListener {
private SwipeRefreshLayout swipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//實例化
swipeLayout = (SwipeRefreshLayout)findViewById(R.id.swp);
swipeLayout.setOnRefreshListener(this);
//設置進度條的主題 swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
//實現OnRefreshListener接口中的方法
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
//這裏實現獲取新的數據,並刷新界面,
............
//刷新數據完畢後,隱藏進度條
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}