PullToRefreshListView是第三方开源框架下拉刷新列表,比较流行的QQ 微信等上面都在用。
下载地址(此开源框架于2013年后不再更新)
package com.lixu.kaiyuanxiala; 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;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private ArrayList<String> date;
private int count = 0;
private ArrayAdapter<String> adapter;
PullToRefreshListView prl; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); date = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, date); prl = (PullToRefreshListView) findViewById(R.id.prl); prl.setAdapter(adapter); TextView tv = new TextView(this);
tv.setText("想看内容给我拉下来!!"); prl.setEmptyView(tv);
// 监控下拉事件 每次下拉执行一次 onRefresh()。
prl.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new MyAsync().execute();
}
}); } private class MyAsync extends AsyncTask {
@Override
protected void onPreExecute() {
// 开始刷新
prl.setRefreshing();
} @Override
protected Object doInBackground(Object... params) { return count++;
} @Override
protected void onPostExecute(Object result) {
Toast.makeText(getApplicationContext(), "更新成功!", 0).show();
date.add(0, "" + result);
// 刷新适配器
adapter.notifyDataSetChanged();
// 结束刷新
prl.onRefreshComplete(); }
}
}
xml文件:
<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" > <com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/prl"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </RelativeLayout>
运行效果图: