在19.1版本之前,Android-PullToRefresh作为最为人知的一个强大的下拉刷新开源三方库,支持各种常用的控件如ListView、GridView、WebView等。但是后来google为我们提供的android-support-v4包,从19.1版本开始,推出了自己的下拉刷新控件SwipeRefreshLayout,作用在多种应用之中。
在讲这两种下拉刷新控件之前,我先说下从android studio项目引入外部库的注意事项(PullToRefresh):
一、导入PullToRefresh
1.将PullToRefresh 从GitHub上下下来,链接地址(https://github.com/chrisbanes/Android-PullToRefresh);
2.工程创建成功,准备导入PullToRefresh库。点击file-->new-->import module
3.在打开的对话框中,选择要导入的library,点击ok。
4.修改Module name的名字,以便于以后自己识别
5.然后点击next-->finish完成
6.完成之后可能会报版本不一致的错误,只需要在对应的库下,找到build.gradle文件,修改成和项目里的版本一致,最后try again,就不报错误了。
7.在自己的App下按F4,选择Dependencice标签,点击加号,选择Module dependency,然后再选择要导入的库,点击OK,等待加载即可。
二、好啦!就这样把PullToRefresh导入项目了,接下来进入实践操作吧。(以PullToRefreshListView为例)
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.testapp.MainActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/myrefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>
2.MainActivity
package com.example.admin.testapp;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//声明
private PullToRefreshListView mPullToRefreshListView;
private ListView listview;
//存放数据的集合
private List<String> mData;
//适配器
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mData=new ArrayList<>();
mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.myrefresh);
listview = mPullToRefreshListView.getRefreshableView();
//模拟从服务器拿来的数据
mData.add("成都");
mData.add("上海");
mData.add("广州");
mData.add("杭州");
mData.add("北京");
mData.add("深圳");
mData.add("西安");
mData.add("天津");
mData.add("兰州");
mData.add("长沙");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mData);
listview.setAdapter(adapter);
//设置刷新监听
mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mData.add("新添加的城市");
adapter.notifyDataSetChanged();
//刷新完成
mPullToRefreshListView.onRefreshComplete();
}
}, 3000);
}
});
}
}
效果图:
三、SwipeRefreshLayout
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.testapp.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/myrefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
2.MainActivity
package com.example.admin.testapp;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
//控件声明
private SwipeRefreshLayout srf;
private ListView listview;
//填充数据的集合
private List<String> mData;
//适配器
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
srf= (SwipeRefreshLayout) findViewById(R.id.myrefresh);
listview= (ListView) findViewById(R.id.listview);
mData=new ArrayList<>();
//模拟服务器填充数据
mData.add("成都");
mData.add("上海");
mData.add("广州");
mData.add("杭州");
mData.add("北京");
mData.add("深圳");
mData.add("西安");
mData.add("天津");
mData.add("兰州");
mData.add("长沙");
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mData);
listview.setAdapter(adapter);
// 设置刷新时进度动画变换的颜色,接收参数为可变长度数组。也可以使用setColorSchemeColors()方法。
srf.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_orange_light, android.R
.color.holo_green_light,android.R.color.holo_red_light);
// 设置刷新时圆形图标的大小。只可传递2个参数,SwipeRefreshLayout.LARGE或SwipeRefreshLayout.DEFAULT。
srf.setSize(SwipeRefreshLayout.DEFAULT);
// 设置刷新时圆形图标的背景色。也可以使用setProgressBackgroundColorSchemeColor()方法。
srf.setProgressBackgroundColorSchemeResource(android.R.color.background_light);
// 设置刷新时的圆形图标。scale:是否缩放;start:圆形图标在刷新前的起始位置;end:圆形图标的偏移量。
srf.setProgressViewOffset(true, 100, 200);
// 设置会触发下拉刷新的手势滑动距离
srf.setDistanceToTriggerSync(100);
// 设置刷新的监听事件
srf.setOnRefreshListener(this);
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mData.add("新添加的城市");
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,"更新列表成功",Toast.LENGTH_SHORT).show();
srf.setRefreshing(false);
}
},3000);
}
}

总结,以上就是这两个下拉刷新的基本运用,当然想要打造更高级、更炫彩的下拉刷新控件,就得去修改它们的源码,自定义各种下拉的样式了。