1、导入library这个工程,然后右击需要使用到PullToRefreshListView的工程,选择Properties,选择android,在右下角点击Add,添加library这个工程。(关于library这个工程可以到网搜:PullToRefresh 。如果真找不到,给我留言,我发给你。)
2、在显示列表的xml配置:
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/showContent"
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" />
3、在Activity中的配置:
a.声明
private List<Map<String, Object>> list = newArrayList<Map<String, Object>>();
private PullToRefreshListView mPullRefreshListView;
private boolean isStart = true;//是否从上往下来
b. mPullRefreshListView =(PullToRefreshListView) findViewById(R.id.showContent);
c.设置下来监听:
// 设定下拉监听函数
mPullRefreshListView
.setOnRefreshListener(newOnRefreshListener<ListView>() {
@Override
public void onRefresh(
PullToRefreshBase<ListView>refreshView) {
Stringlabel = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
|DateUtils.FORMAT_SHOW_DATE
|DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
//refreshView.getCurrentMode()判断上下拖动的方向
if(refreshView.getCurrentMode().equals(Mode.PULL_FROM_START)){
isStart = true;
}
else{
isStart = false;
}
// Do work to refresh the list here.
newGetDataTask().execute();
}
}); mPullRefreshListView.setMode(Mode.BOTH);// 设置底部下拉刷新模式
//listItem = getData();// 获取LIST数据
adapter = new MyAdapter(MainActivity.this,list);
// 设置适配器
mPullRefreshListView.getRefreshableView().setAdapter(adapter);
d.后台处理数据,动态获取数据
private class GetDataTask extends AsyncTask<Void,Void, HashMap<String, Object>> {
//后台处理部分
@Override
protectedHashMap<String, Object> doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
}
List<NameValuePair>params1 = newArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("id", "1"));
params1.add(new BasicNameValuePair("user_id","90"));
HttpPosthttpPost = newHttpPost(PATH);
try {
httpPost.setEntity(newUrlEncodedFormEntity(params1));
HttpResponsehttpResponse = newDefaultHttpClient().execute(httpPost);
int response =httpResponse.getStatusLine().getStatusCode();
data = EntityUtils.toString(httpResponse.getEntity());
}catch(Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HashMap<String,Object> map = newHashMap<String, Object>();
try {
num = num + 1;
map= newHashMap<String, Object>();
map.put("num", "德玛西亚");
map.put("content",data+(num));
}catch(Exception e) {
// TODO: handle exception
setTitle("map出错了");
return null;
}
return map;
}
//这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
//根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
@Override
protected voidonPostExecute(HashMap<String, Object> result) {
//在头部增加新添内容
try {
List<Map<String,Object>> list1 = new ArrayList<Map<String,Object>>();
list1.add(result);
if(isStart){
adapter.addListTop(list1);
}
else{
adapter.addListBottom(list1);
}
//通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
adapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has beenrefreshed.
mPullRefreshListView.onRefreshComplete();
}catch(Exception e) {
// TODO: handle exception
setTitle(e.getMessage());
}
super.onPostExecute(result);
}
}
4、在自定义适配器中添加
public voidaddListTop(List<Map<String, Object>> list1){
list1.addAll(list);
list.removeAll(list);
list.addAll(list1);
}
public voidaddListBottom(List<Map<String, Object>> list1){
list.addAll(list1);
}
以上是参考别人的PullToRefreshListView 实现再根据自己的需要进行修改的,实现了从上往下拖在最顶部加载,从下往上拉在最底部加载。