Android中的延迟加载系列(ListView 3 含完整代码及工程下载)

本节通过一个完整的项目工程,来结束对ListView延迟加载的描述。此项目工程的目的是:数据一共有50行,每一次取得20行显示,在加载下一页时提示正在加载。下面是具体的步骤。


1、建立ListView布局文件empty_list.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/linearLayoutWhole"  
  5.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  6.     android:orientation="vertical">  
  7.   
  8.   
  9.     <ListView android:id="@android:id/list" android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" android:drawSelectorOnTop="false" />  
  11.   
  12. </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>	

以及行布局文件row.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout android:id="@+android:id/iconpref"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_margin="30dip"  
  5.     android:layout_width="fill_parent" android:layout_height="wrap_content"  
  6.     android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">  
  7.   
  8.     <TextView android:id="@+id/title" android:layout_marginLeft="10dip"  
  9.         android:textAppearance="?android:attr/textAppearanceSmall"  
  10.         android:layout_height="wrap_content" android:layout_width="wrap_content"  
  11.         android:textStyle="bold" />  
  12.     <TextView android:id="@+id/description"  
  13.         android:textAppearance="?android:attr/textAppearanceSmall"  
  14.         android:layout_marginLeft="10dip" android:maxLines="2"  
  15.         android:layout_height="wrap_content" android:layout_width="wrap_content"  
  16.         android:layout_below="@+id/title" android:text=""></TextView>  
  17.   
  18. </RelativeLayout>  
<?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行数据)

  1. package com.whyonly.communication;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.whyonly.bean.Row;  
  7.   
  8. public class UIData {  
  9.   
  10.     public static int getTotalRows() {  
  11.         return 50;  
  12.     }  
  13.   
  14.     public static List<Row> getListRows(int startIndex, int endIndex) {  
  15.         List<Row> rows = new ArrayList<Row>();  
  16.         for(int i=startIndex;i<=endIndex;i++){  
  17.             Row row = new Row();  
  18.             row.setTitle("Title "+i);  
  19.             row.setDescription("Title description "+i);  
  20.             rows.add(row);  
  21.         }  
  22.         return rows;  
  23.     }  
  24.   
  25. }  
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对象

  1. package com.whyonly.bean;  
  2.   
  3. public class Row {  
  4.       
  5.     private String title;  
  6.     private String description;  
  7.     public String getTitle() {  
  8.         return title;  
  9.     }  
  10.     public void setTitle(String title) {  
  11.         this.title = title;  
  12.     }  
  13.     public String getDescription() {  
  14.         return description;  
  15.     }  
  16.     public void setDescription(String description) {  
  17.         this.description = description;  
  18.     }  
  19.   
  20. }  
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

  1. package com.whyonly;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.whyonly.bean.Row;  
  6. import com.whyonly.communication.UIData;  
  7. import com.whyonly.core.bean.LazyListData;  
  8. import com.whyonly.core.wrapper.LazyAdapter;  
  9. import com.whyonly.core.wrapper.LazyAdapter.LazyLoading;  
  10. import com.whyonly.core.wrapper.LongOperation;  
  11. import com.whyonly.core.wrapper.LongOperation.Excution;  
  12.   
  13. import android.app.ListActivity;  
  14. import android.os.Bundle;  
  15. import android.os.SystemClock;  
  16. import android.util.Log;  
  17. import android.view.View;  
  18. import android.widget.TextView;  
  19.   
  20. public class LazyLoadingActivity extends ListActivity {  
  21.     private static final String TAG = "LazyLoadingActivity";  
  22.     LazyListData<Row> lazyData;  
  23.    
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);   
  27.         new LongOperation(this,new Excution(){  
  28.             @Override  
  29.             public void longExcute(){     
  30.                 lazyData = new LazyListData<Row>(UIData.getTotalRows(),  
  31.                         UIData.getListRows(0,LazyAdapter.PAGE_SIZE_LAZY-1));  
  32.                 SystemClock.sleep(3000);//休息3秒,模拟网络延迟   
  33.             }  
  34.             @Override  
  35.             public void uiUpdate(){       
  36.                 setContentView(R.layout.empty_list);          
  37.                 setListAdapter(new LazyAdapter<Row>(  
  38.                         LazyLoadingActivity.this,  
  39.                         R.layout.row,//list中的行布局   
  40.                         lazyData.getListData(),//得到数据   
  41.                         lazyData.getTotalRows(),//得到总行数   
  42.                         new LazyLoading(){  
  43.                             @Override  
  44.                             public void cacheNextPageData(int startIndex, int endIndex) {//加载下一页   
  45.                                 Log.d(TAG,"cacheNextPageData() startIndex="+startIndex+", endIndex="+endIndex);  
  46.                                 List<Row> nextList = UIData.getListRows(startIndex,endIndex);  
  47.                                 lazyData.getListData().addAll(nextList);   
  48.                                 SystemClock.sleep(3000);//休息3秒,模拟网络延迟   
  49.                             }  
  50.                             @Override  
  51.                             public void updateItemView(View convertView, Object bean) {//更新每一行   
  52.                                 updateItem(convertView, (Row) bean);  
  53.                             }     
  54.                         }  
  55.                 ));  
  56.             }  
  57.         }).execute(new String[]{});  
  58.           
  59.     }  
  60.   
  61.       
  62.     private void updateItem(final View vi,final Row bean){  
  63.         if (bean != null) {  
  64.             TextView title = (TextView) vi.findViewById(R.id.title);  
  65.             TextView description = (TextView) vi.findViewById(R.id.description);  
  66.             title.setText(bean.getTitle());  
  67.             description.setText(bean.getDescription());  
  68.         }  
  69.     }  
  70. }  
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,

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.whyonly"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="7" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".LazyLoadingActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18. </manifest>  
<?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,基本上可以满足要求了。但是当行数据包括图片或者其它占用较大带宽的网络资源时,就有可能要花费较长的时间才能获取完毕。这时候可以采取进一步的优化,对行数据中的图片再次使用延迟加载技术,即获取文本数据之后,立即显示整个页面,然后从远程逐个获取图片资源并显示。本系列文章接下来的部分将对此进行进一步的讲解。(待续)


完整的工程代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值