上一节讲述了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>
<?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
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[]{});
- }
@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()方法是封装之后的延迟加载适配,参见程序代码说明。
下一节将通过一个完整项目进行展示。(待续)