本节通过一个完整的项目工程,来结束对ListView延迟加载的描述。此项目工程的目的是:数据一共有50行,每一次取得20行显示,在加载下一页时提示正在加载。下面是具体的步骤。
1、建立ListView布局文件empty_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutWhole"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:drawSelectorOnTop="false" />
</LinearLayout>
以及行布局文件row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+android:id/iconpref"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_margin="30dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
<TextView android:id="@+id/title" android:layout_marginLeft="10dip"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" />
<TextView android:id="@+id/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dip" android:maxLines="2"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@+id/title" android:text=""></TextView>
</RelativeLayout>
2,准备页面的延迟数据。(共50行数据)
package com.whyonly.communication;
import java.util.ArrayList;
import java.util.List;
import com.whyonly.bean.Row;
public class UIData {
public static int getTotalRows() {
return 50;
}
public static List<Row> getListRows(int startIndex, int endIndex) {
List<Row> rows = new ArrayList<Row>();
for(int i=startIndex;i<=endIndex;i++){
Row row = new Row();
row.setTitle("Title "+i);
row.setDescription("Title description "+i);
rows.add(row);
}
return rows;
}
}
Row对象
package com.whyonly.bean;
public class Row {
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
3,实现Activity
package com.whyonly;
import java.util.List;
import com.whyonly.bean.Row;
import com.whyonly.communication.UIData;
import com.whyonly.core.bean.LazyListData;
import com.whyonly.core.wrapper.LazyAdapter;
import com.whyonly.core.wrapper.LazyAdapter.LazyLoading;
import com.whyonly.core.wrapper.LongOperation;
import com.whyonly.core.wrapper.LongOperation.Excution;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class LazyLoadingActivity extends ListActivity {
private static final String TAG = "LazyLoadingActivity";
LazyListData<Row> lazyData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LongOperation(this,new Excution(){
@Override
public void longExcute(){
lazyData = new LazyListData<Row>(UIData.getTotalRows(),
UIData.getListRows(0,LazyAdapter.PAGE_SIZE_LAZY-1));
SystemClock.sleep(3000);//休息3秒,模拟网络延迟
}
@Override
public void uiUpdate(){
setContentView(R.layout.empty_list);
setListAdapter(new LazyAdapter<Row>(
LazyLoadingActivity.this,
R.layout.row,//list中的行布局
lazyData.getListData(),//得到数据
lazyData.getTotalRows(),//得到总行数
new LazyLoading(){
@Override
public void cacheNextPageData(int startIndex, int endIndex) {//加载下一页
Log.d(TAG,"cacheNextPageData() startIndex="+startIndex+", endIndex="+endIndex);
List<Row> nextList = UIData.getListRows(startIndex,endIndex);
lazyData.getListData().addAll(nextList);
SystemClock.sleep(3000);//休息3秒,模拟网络延迟
}
@Override
public void updateItemView(View convertView, Object bean) {//更新每一行
updateItem(convertView, (Row) bean);
}
}
));
}
}).execute(new String[]{});
}
private void updateItem(final View vi,final Row bean){
if (bean != null) {
TextView title = (TextView) vi.findViewById(R.id.title);
TextView description = (TextView) vi.findViewById(R.id.description);
title.setText(bean.getTitle());
description.setText(bean.getDescription());
}
}
}
把Activity加入Manifast,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whyonly"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LazyLoadingActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
运行工程,页面如下所示:
扩展和思考:在此项目中,每一次只取得20行数据,对于包含较大数据量的ListView,基本上可以满足要求了。但是当行数据包括图片或者其它占用较大带宽的网络资源时,就有可能要花费较长的时间才能获取完毕。这时候可以采取进一步的优化,对行数据中的图片再次使用延迟加载技术,即获取文本数据之后,立即显示整个页面,然后从远程逐个获取图片资源并显示。本系列文章接下来的部分将对此进行进一步的讲解。(待续)
完整的工程代码 下载