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);
}
}