上一節講述了Listview延遲加載的封裝,本節將講述如何在Activity中進行調用。
首先定義一個ListView佈局
<?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>
其次定義一個ListActivity,引入上述佈局
public class LazyLoadingActivity extends ListActivity
在onCreate() 方法中加載數據和佈局,LongOperation更多的相信信息請參照 頁面(Activity)之間的平滑跳轉及封 法中加載數據
@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[]{});
}
new LazyAdapter()方法是封裝之後的延遲加載適配,參見程序代碼說明。
下一節將通過一個完整項目進行展示。(待續)