介绍
什么是SwipeRefreshLayout?SwipeRefreshLayout是Google官方提供的一个下拉刷新控件,可在android v4包中获得,兼容android 1.6以上版本。
基本用法
添加SwipeRefreshLayout控件
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
响应下拉刷新动作,设置刷新回调
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");
initiateRefresh();
}
});
private void initiateRefresh() {
Log.i(LOG_TAG, "initiateRefresh");
/**
* Execute the background task, which uses {@link android.os.AsyncTask} to load the data.
*/
new DummyBackgroundTask().execute();
}
//数据加载完,停止刷新指示
// Stop the refreshing indicator
mSwipeRefreshLayout.setRefreshing(false);
更改刷新控件的样式,包括下拉高度,颜色,大小等。
//更改控件指示颜色
mSwipeRefreshLayout.setColorScheme(
R.color.swipe_color_1, R.color.swipe_color_2,
R.color.swipe_color_3, R.color.swipe_color_4);
//设置控件大小
mSwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//设置下拉高度
mSwipeRefreshLayout.setProgressViewEndTarget(true,300);