1.创建一个新的工程,取名为listViewDemo
2.创建工程完毕,引用第三方工程项目作为库。(这个例子引用了开源的pulltorefresh工程,这个工程可以网上下载)代码如下:
package com.example.listviewdemo;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private PullToRefreshListView ptrlistview;
private ArrayAdapter<string> aa;
private ListView actualListView;
public LinkedList<string> ll;
private String[] arr_demo;
private static int n = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
setAdapter();
setEevent();
}
private void setEevent() {
ptrlistview.setOnRefreshListener(new OnRefreshListener<listview>() {
@Override
public void onRefresh(PullToRefreshBase<listview> refreshView) {
//下拉刷新时,显示时间日期等
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
//异步处理数据
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
}
private void setAdapter() {
//3.给数据设置适配样式
aa = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1, ll);
//将适配好的内容添加到可刷新的listview中
actualListView.setAdapter(aa);
}
private void initData() {
//2.初始化数据,给后面的动态数据加
ll = new LinkedList<string>();
arr_demo = new String[30];
for(int i = 29;i>0;i--)
{
arr_demo[i]="demo"+i;
ll.add(arr_demo[i]);
}
}
private void initView() {
//1.得到一个可以刷新的listview,这里用外部的pulltorefresh库,动态改变样式
ptrlistview =(PullToRefreshListView)findViewById(R.id.ptrlistview);
actualListView=ptrlistview.getRefreshableView();
}
private class GetDataTask extends AsyncTask<void void="" string="">
{
@Override
protected String doInBackground(Void... params) {
try
{
Thread.sleep(2000);
}
catch(Exception e)
{
e.printStackTrace();
}
String result ="demo"+ n++;
return result;
}
@Override
protected void onPostExecute(String result) {
//往头部新增内容
ll.addFirst(result);
//通知程序数据集已经改变,如果不做通知,那么将不会刷新数组
aa.notifyDataSetChanged();
//刷新完毕
ptrlistview.onRefreshComplete();
super.onPostExecute(result);
}
}
}
</void></string></string></listview></listview></string></string>
此处是布局文件代码:
<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"
tools:context=".MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/ptrlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
效果如图: