效果预览图:

下拉刷新--第三方开源--PullToRefresh-LMLPHP

下载地址:https://github.com/chrisbanes/Android-PullToRefresh

activity_main.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"
tools:context="com.zzw.testpulltorefresh.MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout>

MainActuvity.java:

 package com.zzw.testpulltorefresh;

 import java.util.ArrayList;
import java.util.Date; 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.os.SystemClock;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private PullToRefreshListView listView;
private ArrayList<String> data;
private ArrayAdapter adapter;
private int count = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); data = new ArrayList<String>(); listView = (PullToRefreshListView) findViewById(R.id.listView);
// 设置向下滑动时刷新
listView.setMode(Mode.PULL_FROM_START);
// 支持下拉和上拉 listView.setMode(Mode.BOTH);
// 设置监听
listView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// 在这完成业务逻辑
new MyAsyncTask().execute();
}
}); adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter); // 设置如果数据为空的时候显示什么
TextView textView = new TextView(this);
textView.setText("请下拉刷新");
listView.setEmptyView(textView);
} private class MyAsyncTask extends AsyncTask { @Override
protected void onPreExecute() {
// 开始刷新
listView.setRefreshing();
} @Override
protected Object doInBackground(Object... params) {
// 假设耗时时间为3秒
SystemClock.sleep(3000);
return count++;
} @Override
protected void onPostExecute(Object result) { data.add(0, result + "");
adapter.notifyDataSetChanged(); // 设置标签
listView.setLastUpdatedLabel("最后更新新的时间:" + new Date()); // 刷新完成
listView.onRefreshComplete();
Toast.makeText(getApplicationContext(), "加载成功", 0).show();
}
} }
04-24 18:56