本節通過一個完整的項目工程,來結束對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,基本上可以滿足要求了。但是當行數據包括圖片或者其它佔用較大帶寬的網絡資源時,就有可能要花費較長的時間才能獲取完畢。這時候可以採取進一步的優化,對行數據中的圖片再次使用延遲加載技術,即獲取文本數據之後,立即顯示整個頁面,然後從遠程逐個獲取圖片資源並顯示。本系列文章接下來的部分將對此進行進一步的講解。(待續)
完整的工程代碼 下載