Android中的延迟加载系列(ListView 1)

来源:互联网 发布:怀化学院教育网络系统 编辑:程序博客网 时间:2024/06/05 06:27

在Java语言开发中,大家对延迟加载并不陌生,比如Hibernate开发中就大量用到了延迟加载的技术。延迟加载在Android中同样适用,其基本思想是:只有在必要的时候,才去加载数据项。

本系列文章将对Android中常用的延迟加载进行简要的总结,并给出示例代码,以帮助读者的理解。由于时间的限制,本系列文章只讲解Android中的ListView和ImageView,其它涉及的组件较为类似,因此不一一展开。

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。当需要从服务器获取大量数据的时候,为了节约用户的网络流量负载以及提高大数据量操作和显示的速度,这个时候往往需要用到延迟加载的技术。


ListView的延迟加载技术中,endless比较有名,大家可以到https://github.com/commonsguy/cwac-endless下载相应的jar,作为项目的类库。由于网上有很多endless相关的文章,因此本文不再累述。本文的主要目的是封装endless,寻找一种更加简易、清晰和可扩展的方式,以求达到对ListView的延迟加载。下文逐步阐述:


首先定义个延迟加载数据类(LazyListData.java),此类非常简单,抽象了List数据的总行数totalRows以及当前已有的行数据listData. List的数据类型通过<T>泛型(也可以用别的字母)的方式来定义。Serializable表示数据的可序列化。

[java] view plaincopyprint?
  1. import java.util.List;  
  2.   
  3. public class LazyListData<T> implements java.io.Serializable{  
  4.   
  5.     private static final long serialVersionUID = 296133151360323907L;  
  6.     private int totalRows;  
  7.     private List<T> listData;  
  8.       
  9.       
  10.     public LazyListData(int totalRows,List<T> listData){  
  11.         this.totalRows = totalRows;  
  12.         this.listData = listData;  
  13.     }  
  14.       
  15.       
  16.     public int getTotalRows() {  
  17.         return totalRows;  
  18.     }  
  19.     public void setTotalRows(int totalRows) {  
  20.         this.totalRows = totalRows;  
  21.     }  
  22.     public List<T> getListData() {  
  23.         return listData;  
  24.     }  
  25.     public void setListData(List<T> listData) {  
  26.         this.listData = listData;  
  27.     }  
  28.   
  29. }  

其次定义个延迟加载的接口(LazyLoading.java),此类抽象了获取下一页数据的方法以及获取数据后更新相应页面的方法。startIndex是下一页数据相对于整体数据的开始索引,endindex是下一页相对于整体数据数据的结束索引


[java] view plaincopyprint?
  1. public interface LazyLoading{  
  2.         public void cacheNextPageData(int startIndex,int endIndex);  
  3.         public void updateItemView(View convertView, Object bean);  
  4. }  

再次对EndlessAdapter进行封装(LazyAdapter.java),参见以下代码。

[java] view plaincopyprint?
  1. package com.whyonly.wrapper;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.util.Log;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.ListView;  
  12.   
  13. import com.commonsware.cwac.endless.EndlessAdapter;  
  14. import com.whyonly.R;  
  15. import com.whyonly.wrapper.LazyAdapter.LazyLoading;  
  16.   
  17. public class LazyAdapter<T> extends EndlessAdapter{  
  18.     private static final String TAG = "LazyAdapter";  
  19.     private static final int PAGE_SIZE_LAZY = 20;  
  20.     private Context mContext;  
  21.     private int mItemResId;  
  22.     private int mStartIndex_nextPage = 0;  
  23.     private int mPageSize = 20;  
  24.     private int mTotalRows = 0;  
  25.     private LazyLoading mLazyLoading;  
  26.     private ListView mListView;  
  27.       
  28.     public LazyAdapter(ListView listView,Context context,int itemResId,List<T> list,int pageSize,int totalRows,LazyLoading lazyLoading) {   
  29.         super(new MyArrayAdapter1<T>(context,itemResId,list,lazyLoading));  
  30.         Log.d(TAG,"LazyAdapter()");  
  31.         mListView = listView;  
  32.         mContext = context;  
  33.         mItemResId = itemResId;  
  34.         mStartIndex_nextPage = list.size();  
  35.         mPageSize = pageSize;  
  36.         mTotalRows = totalRows;  
  37.         mLazyLoading = lazyLoading;   
  38.     }  
  39.       
  40.     public LazyAdapter(ListView listView,Context context,int itemResId,List<T> list,int totalRows,LazyLoading lazyLoading) {  
  41.         this(listView,context,itemResId,list,PAGE_SIZE_LAZY,totalRows,lazyLoading);  
  42.     }  
  43.       
  44.     public LazyAdapter(Context context,int itemResId,List<T> list,int totalRows,LazyLoading lazyLoading) {  
  45.         this(null,context,itemResId,list,PAGE_SIZE_LAZY,totalRows,lazyLoading);  
  46.     }  
  47.       
  48.       
  49.     public interface LazyLoading{  
  50.         public void cacheNextPageData(int startIndex,int endIndex);  
  51.         public void updateItemView(View convertView, Object bean);  
  52.     }  
  53.       
  54.     @Override  
  55.     protected View getPendingView(ViewGroup parent) {  
  56.         Log.d(TAG,"getPendingView()");  
  57.         LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  58.         View row=vi.inflate(R.layout.progressrow, null);  
  59.         if(mStartIndex_nextPage >= mTotalRows){//hidden the recycle  
  60.             row.findViewById(R.id.progressBar1).setVisibility(View.GONE);  
  61.             row.setVisibility(View.GONE);  
  62.         }     
  63.         return row;  
  64.     }  
  65.       
  66.     @Override  
  67.     protected boolean cacheInBackground() {  
  68.         Log.d(TAG,"getWrappedAdapter().getCount()="+getWrappedAdapter().getCount()+",mTotalRows="+mTotalRows);  
  69.         if(getWrappedAdapter().getCount()<mTotalRows){  
  70.             int endIndex_nextPage = Math.min(mStartIndex_nextPage + mPageSize -1,mTotalRows-1);  
  71.             mLazyLoading.cacheNextPageData(mStartIndex_nextPage,endIndex_nextPage);  
  72.             mStartIndex_nextPage = endIndex_nextPage + 1;  
  73.             return true;  
  74.         }else{  
  75.             Log.d(TAG,"cacheInBackground() return false");  
  76.             return false;  
  77.         }  
  78.     }  
  79.       
  80.     @Override  
  81.     protected void appendCachedData() {  
  82.     }  
  83. }  
  84.   
  85. class MyArrayAdapter1<T> extends ArrayAdapter<T> {  
  86.   
  87.     private Context mContext;  
  88.     private LazyLoading mLazyLoading;  
  89.     private int mItemResId;   
  90.       
  91.     public MyArrayAdapter1(Context context, int itemResId, List<T> items,LazyLoading lazyLoading) {  
  92.             super(context, 0, items);  
  93.             this.mContext = context;  
  94.             mItemResId = itemResId;  
  95.             this.mLazyLoading = lazyLoading;  
  96.     }  
  97.   
  98.     @Override  
  99.     public View getView(int position, View convertView, ViewGroup parent) {  
  100.             View item = convertView;  
  101.             if (item == null) {  
  102.                 LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  103.                 item = vi.inflate(mItemResId, null);  
  104.             }    
  105.             final T bean = getItem(position);     
  106.             mLazyLoading.updateItemView(item, bean);  
  107.             return item;  
  108.     }  
  109. }  

在此类中,R.layout.progressrow表示加载下一页代码的等待视图,等待视图的目的是提示用户正在加载,使得页面比较平滑和增加用户友好性,布局代码如下:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:id="@+id/linearLayout1"  
  3.     android:layout_width="fill_parent" android:layout_height="wrap_content"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:gravity="center"  
  6.     >  
  7.     <ProgressBar android:id="@+id/progressBar1"  android:paddingTop="3dip"  
  8.         android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar>  
  9. </LinearLayout>  

至此,Listview延迟加载的封装基本结束,接下来将通过一个示例讲解如何在Activity中高效地应用此封装。(待续)
原创粉丝点击