首先去这个地址https://github.com/chrisbanes/Android-PullToRefresh,下载这个项目的zip压缩包,解压出来后把library这个文件夹导入eclipse里
在eclipse里设置一下
接下在创建的项目里,引用这个library
这个项目的布局
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</LinearLayout>
public class MainActivity extends ListActivity {
private LinkedList<String> listItems;
private PullToRefreshListView pullRefreshListView;
private ArrayAdapter<String> arrayAdapter;
private String[] mStrings = { "Abbaye de Belloc",
"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// 下拉刷新监听
pullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
// 获取系统时间,xx月xx日 xx:xx
String label = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// 显示最后更新的时间
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 异步更新列表
new GetDataTask().execute();
}
});
// 列表下拉到最后位置监听
pullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(MainActivity.this, "到底啦!!!!!",
Toast.LENGTH_SHORT).show();
}
});
ListView actualListView = pullRefreshListView.getRefreshableView();
registerForContextMenu(actualListView);
listItems = new LinkedList<String>();
listItems.addAll(Arrays.asList(mStrings));
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems);
// 可以用setListAdapter(mAdapter)或者
// pullRefreshListView.setAdapter(mAdapter)
actualListView.setAdapter(arrayAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
System.out.println(position + "-------" + listItems.get(position - 1));
super.onListItemClick(l, v, position, id);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
// 主要负责执行那些很耗时的后台处理工作
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
return mStrings;
}
// 在doInBackground 执行完成后,onPostExecute 方法将被UI 线程调用,后台的计算结果将通过该方法传递到UI
// 线程,并且在界面上展示给用户.
protected void onPostExecute(String[] result) {
// 每刷新一次在列表的最前面添加一个item:Added after refresh...
listItems.addFirst("Added after refresh...");
// notifyDataSetChanged方法通过一个外部的方法控制,如果适配器的内容改变时需要强制调用getView来刷新每个Item的内容
arrayAdapter.notifyDataSetChanged();
// 当列表刷新完成后回调onRefreshComplete
pullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}