首先到此网址下载第三方包https://github.com/chrisbanes/Android-PullToRefresh
下载完成之后找到library文件夹 将此文件夹导入到eclipse当中并勾选islibraray。新建的工程里面add library;
然后开始写代码
package com.example.pulltorefresh1;
import java.util.ArrayList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ArrayList<String> data;
PullToRefreshListView pulltorefresh;
ArrayAdapter<String> array;
private int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<String>();
pulltorefresh = (PullToRefreshListView) findViewById(R.id.pulll);
pulltorefresh.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new MyTask().execute();
}
});
array = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
pulltorefresh.setAdapter(array);
}
public class MyTask extends AsyncTask {
@Override
protected Object doInBackground(Object... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count++;
}
@Override
protected void onPostExecute(Object result) {
data.add(0, result + "");
array.notifyDataSetChanged();
pulltorefresh.onRefreshComplete();
}
}
}
接下来是布局文件
<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.pulltorefresh1.MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pulll"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>