package com.example.pulltorefresh;
import java.util.ArrayList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
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;
import android.widget.TextView;
public class MainActivity extends Activity {
private ArrayList<String> data;
private int count=0;
private ArrayAdapter adapter;
private PullToRefreshListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=new ArrayList<String>();
mListView=(PullToRefreshListView) findViewById(R.id.list_view);
mListView.setMode(Mode.PULL_FROM_START);
mListView.setOnRefreshListener(new OnRefreshListener<ListView>(){
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
//完成你的业务逻辑
new MyTask().execute();
}});
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
mListView.setAdapter(adapter);
TextView text=new TextView(this);
text.setText("请下拉刷新");
mListView.setEmptyView(text);
}
private class MyTask extends AsyncTask{
@Override
protected void onPreExecute() {
mListView.setRefreshing();
}
@Override
protected Object doInBackground(Object... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return count++;
}
@Override
protected void onPostExecute(Object result) {
data.add(0, "更新数据"+result);
mListView.setLastUpdatedLabel("最后更新时间:11月24日16:04"+System.currentTimeMillis());
adapter.notifyDataSetChanged();
mListView.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"
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="com.example.pulltorefresh.MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
开源来源(jithub):https://github.com/chrisbanes/Android-PullToRefresh