因为公司业务调整,已经很久没有做Android这块了.最近刚好手上有个Android项目要用到下拉刷新.之前一直是用别人自定义写的PullToRefreshView,不过后面谷歌自己出了下拉刷新.之前用了感觉还行,这次也用它.不过好久没用了,发现网站中也没有相关的文章.于是乎自己写一个喽.首先须把你的support library的版本升级到19.1或更新.不然没有SwipeRefreshLayout这个控件.升级好之后.先布局,后写代码事件.
布局
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="@string/swipe_to_refresh"
android:textSize="20sp"
android:textStyle="bold" />
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Java代码
layout_swipe = (SwipeRefreshLayout) findViewById(R.id.layout_swipe);
layout_swipe.setOnRefreshListener(this);
layout_swipe.setColorSchemeResources(android.R.color.holo_blue_light,
android.R.color.holo_red_light,
android.R.color.holo_orange_light,
android.R.color.holo_green_light);
setColorScheme()已经弃用,使用setColorSchemeResources()来设置颜色。不少人还在用前面的方法.
事件代码
@Override
public void onRefresh() {
// TODO Auto-generated method stub
layout_swipe.setRefreshing(true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
layout_swipe.setRefreshing(false);
MLog.makeText("刷新");
}
}, 3000);
}
是不是很简单呢!

本文链接:https://www.it72.com/11746.htm