Android学习笔记_37_ListView批量加载数据和页脚设置

1、在activity_main.xml布局文件中加入ListView控件:

<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=".MainActivity" > <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView> </RelativeLayout>

2、为ListView添加项listview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout>

3、添加页脚界面footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ProgressBar
android:layout_width="50dp"
android:layout_height="match_parent"
style="?android:attr/progressBarStyle"
android:id="@+id/downloadbar"
/> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="数据正在加载中" /> </LinearLayout>

4、后台代码实现:

package com.example.listviewbatch;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity {
private static final String TAG="ListMainActivity";
private ListView listView;
private List<String> data = new ArrayList<String>();
private int pageCount=30;
private int maxPage;
private boolean loadfinish=true;//加载完毕
private ArrayAdapter<String> adapter;
private View footer;//页脚
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = DataService.getData(1, pageCount);
maxPage=DataService.getTotal()/pageCount==0?DataService.getTotal()/pageCount:DataService.getTotal()/pageCount+1;
listView = (ListView) this.findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,R.layout.listview_item
,R.id.textView,data);
footer =getLayoutInflater().inflate(R.layout.footer, null);
listView.addFooterView(footer);
listView.setAdapter(adapter);
listView.removeFooterView(footer);
//监听ListView滚动事件
listView.setOnScrollListener(new MyScrollListener());
}
private final class MyScrollListener implements OnScrollListener{ @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.i(TAG, "onScrollStateChanged( scrollState: "+scrollState+")");
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.i(TAG, "onScroll( firstVisibleItem: "+firstVisibleItem+")"+
"visibleItemCount: "+visibleItemCount+")"+
"totalItemCount: "+totalItemCount+")");
//获取屏幕最后行索引
int lastItemId = listView.getLastVisiblePosition();
final int total= totalItemCount;
if((lastItemId+1)==totalItemCount){//换页
if(totalItemCount>0){
int currentPage = totalItemCount/pageCount==0?totalItemCount/pageCount:totalItemCount/pageCount+1;
if((currentPage+1)<maxPage && loadfinish){
loadfinish=false;
listView.addFooterView(footer);
//开启子线程获取数据,由于子线程不能更新主线程的UI,所以必须通过Handle进行处理,需要子线程将数据包装
//成Message对象,再通过Handle接收到数据,调用notifyDataSetChanged通知适配器更
//新ListView
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<String> resultList =DataService.getData(total, pageCount);
Log.i("DataServiceGetData","count: "+resultList.size());
handler.sendMessage(handler.obtainMessage(0, resultList));
}
}).start();
}
}
}
}
}
private Handler handler = new Handler(){ @Override
public void handleMessage(Message msg) {
data.addAll((List<String>) msg.obj);
//通知ListView数据发生改变,要求ListView更新界面
adapter.notifyDataSetChanged();
if(listView.getFooterViewsCount()>0){
listView.removeFooterView(footer);
}
loadfinish=true;
} };
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
public class DataService {
public static List<String> getData(int items,int count){
int j=0;
List<String> dataList = new ArrayList<String>();
for (int i = items; i <= 200;i++) {
j=j+1;
if(j<count){
dataList.add("ListView批量加载数据"+i);
}
}
return dataList;
}
public static int getTotal(){
return 200;
}
}
上一篇:Session和Cookie介绍及常见httpcode


下一篇:Django session/cookie