PullToRefresh是一个开源的第三方下拉刷新、上拉加载控件
下载
https://github.com/chrisbanes/Android-PullToRefresh,点击Download ZIP下载。
导入项目
下载完成后,解压文件。
我使用的AndroidStudio,点击file-new-import module,选中已解压的文件夹,
选择完成后,AS可能会有导入项目的提示,直接按默认选择就好。
添加到自己的项目
导入后AS会进行Gradle配置,配置完成后选中自己的项目按F4或者右键Open Module Settings
在弹出的对话框上,选择Dependencies,点击右边的加号–module dependency,选择刚才导入的项目。
这样之后就能在自己的项目中使用PullToRefresh了。
使用
先在布局中添加
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="demo.ado.com.pulltorefresh.MainActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</RelativeLayout>
Activity中使用
然后直接在Activity中就可以了使用了,就像使用ListView一样。
package demo.ado.com.pulltorefresh;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView pullToRefreshListView;
private ArrayList<String> list=new ArrayList<String>();;
int a=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
}
private void initUi() {
addLine();
//实例化pull
pullToRefreshListView= (PullToRefreshListView) findViewById(R.id.pull);
//设置上拉和下拉
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
pullToRefreshListView.setAdapter(adapter);
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new GetDataTask().execute();
//加载数据后,更新adapter。
adapter.notifyDataSetChanged();
}
});
}
/**
**刷新一次就加载10行
**/
private void addLine() {
for (int i = 0; i <10 ; i++) {
list.add("这是第"+a+"行数啊");
a++;
}
}
private class GetDataTask extends AsyncTask<Void,Void,String[]>{
@Override
protected String[] doInBackground(Void... voids) {
//模拟数据加载,停止2秒
SystemClock.sleep(2000);
//然后添加10行数据
addLine();
return null;
}
@Override
protected void onPostExecute(String[] result) {
pullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
效果图