在android開發中Listview是一個很重要的組件,它以列表的形式根據數據的長自適應展現具體內容。php
用戶能夠自由的定義listview每一列的佈局,但當listview有大量的數據須要加載的時候,會佔據大量內存,影響性能,這時候就須要按需填充並從新使用view來減小對象的建立。android
ListView加載數據都是在緩存
1 public View getView(int position, View convertView, ViewGroup parent) { 2 3 。。。。。。 4 5 }
方法中進行的(要自定義listview都須要重寫listadapter:如 BaseAdapter,SimpleAdapter,CursorAdapter的等的getvView方法),性能優化
優化listview的加載速度就要讓 convertView匹配列表類型,並最大程度上的從新使用convertView。ide
其中,getview的加載方法通常有如下三種加載方式:佈局
一、最慢的加載方式是每一次都從新定義一個View載入佈局,再加載數據性能
1 public View getView(int position, View convertView, ViewGroup parent) { 2 3 View item = mInflater.inflate(R.layout.list_item_icon_text, null); 4 5 ((TextView) item.findViewById(R.id.text)).setText(DATA[position]); 6 7 ((ImageView) item.findViewById(R.id.icon)).setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 8 9 return item; 10 11 }
二、正確的加載方式是當convertView不爲空的時候直接從新使用convertView從而減小了不少沒必要要的View的建立,而後加載數據優化
1 public View getView(int position, View convertView, ViewGroup parent) { 2 3 if (convertView == null) { 4 5 convertView = mInflater.inflate(R.layout.item, parent, false); 6 7 } 8 9 ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]); 10 11 ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 12 13
14 15 return convertView; 16 17 }
三、最快的方式是定義一個ViewHolder,將convetView的tag設置爲ViewHolder,不爲空時從新使用便可google
1 static class ViewHolder { 2 3 TextView text; 4 5 ImageView icon; 6 7 } 8 9 10 11 public View getView(int position, View convertView, ViewGroup parent) { 12 13 ViewHolder holder; 14 15 if (convertView == null) { 16 17 convertView = mInflater.inflate(R.layout.list_item_icon_text,parent, false); 18 19 holder = new ViewHolder(); 20 21 holder.text = (TextView) convertView.findViewById(R.id.text); 22 23 holder.icon = (ImageView) convertView.findViewById(R.id.icon); 24 25 convertView.setTag(holder); 26 27 } else { 28 29 holder = (ViewHolder) convertView.getTag(); 30 31 } 32 33 holder.text.setText(DATA[position]); 34 35 holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 :mIcon2); 36 37 return convertView; 38 39 }
三種方式加載效率對好比下圖所示:spa
說明:上述三個例子代碼摘自google 2010 I/O大會
當處理一些耗時的資源加載的時候須要作到如下幾點,以使你的加載更快更平滑:
1. 適配器在界面主線程中進行修改
2. 能夠在任何地方獲取數據但應該在另一個地方請求數據
3. 在主界面的線程中提交適配器的變化並調用notifyDataSetChanged()方法
===============================分割線=========================================
那麼若是存在多個item樣式如何處理呢??
大體思路就是建立多個viewholder,在getViewType的時候設置不一樣位置的item用不一樣的viewholder,
如下直接上代碼:
1 class MyAdapter extends BaseAdapter{ 2 3 Context mContext; 4 LinearLayout linearLayout = null; 5 LayoutInflater inflater; 6 TextView tex; 7 final int VIEW_TYPE = 3; 8 final int TYPE_1 = 0; 9 final int TYPE_2 = 1; 10 final int TYPE_3 = 2; 11 12 //各個佈局的控件資源 13 class viewHolder1{ 14 CheckBox checkBox; 15 TextView textView; 16 } 17 class viewHolder2{ 18 TextView textView; 19 } 20 class viewHolder3{ 21 ImageView imageView; 22 TextView textView; 23 } 24 25 public MyAdapter(Context context) { 26 // TODO Auto-generated constructor stub 27 mContext = context; 28 inflater = LayoutInflater.from(mContext); 29 } 30 31 @Override 32 public int getCount() { 33 // TODO Auto-generated method stub 34 return listString.size(); 35 } 36 37 //每一個convert view都會調用此方法,得到當前所須要的view樣式 38 @Override 39 public int getItemViewType(int position) { 40 // TODO Auto-generated method stub 41 int p = position%6; 42 if(p == 0) 43 return TYPE_1; 44 else if(p < 3) 45 return TYPE_2; 46 else if(p < 6) 47 return TYPE_3; 48 else 49 return TYPE_1; 50 } 51 52 //返回樣式的數量 53 @Override 54 public int getViewTypeCount() { 55 // TODO Auto-generated method stub 56 return 3; 57 } 58 59 @Override 60 public Object getItem(int arg0) { 61 // TODO Auto-generated method stub 62 return listString.get(arg0); 63 } 64 65 @Override 66 public long getItemId(int position) { 67 // TODO Auto-generated method stub 68 return position; 69 } 70 71 @Override 72 public View getView(int position, View convertView, ViewGroup parent) { 73 // TODO Auto-generated method stub 74 viewHolder1 holder1 = null; 75 viewHolder2 holder2 = null; 76 viewHolder3 holder3 = null; 77 int type = getItemViewType(position); 78 79 80 //無convertView,須要new出各個控件 81 if(convertView == null) 82 { 83 Log.e("convertView = ", " NULL"); 84 85 //按當前所需的樣式,肯定new的佈局 86 switch(type) 87 { 88 case TYPE_1: 89 convertView = inflater.inflate(R.layout.listitem1, parent, false); 90 holder1 = new viewHolder1(); 91 holder1.textView = (TextView)convertView.findViewById(R.id.textview1); 92 holder1.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox); 93 Log.e("convertView = ", "NULL TYPE_1"); 94 convertView.setTag(holder1); 95 break; 96 case TYPE_2: 97 convertView = inflater.inflate(R.layout.listitem2, parent, false); 98 holder2 = new viewHolder2(); 99 holder2.textView = (TextView)convertView.findViewById(R.id.textview2); 100 Log.e("convertView = ", "NULL TYPE_2"); 101 convertView.setTag(holder2); 102 break; 103 case TYPE_3: 104 convertView = inflater.inflate(R.layout.listitem3, parent, false); 105 holder3 = new viewHolder3(); 106 holder3.textView = (TextView)convertView.findViewById(R.id.textview3); 107 holder3.imageView = (ImageView)convertView.findViewById(R.id.imageview); 108 Log.e("convertView = ", "NULL TYPE_3"); 109 convertView.setTag(holder3); 110 break; 111 } 112 } 113 else 114 { 115 //有convertView,按樣式,取得不用的佈局 116 switch(type) 117 { 118 case TYPE_1: 119 holder1 = (viewHolder1) convertView.getTag(); 120 Log.e("convertView !!!!!!= ", "NULL TYPE_1"); 121 break; 122 case TYPE_2: 123 holder2 = (viewHolder2) convertView.getTag(); 124 Log.e("convertView !!!!!!= ", "NULL TYPE_2"); 125 break; 126 case TYPE_3: 127 holder3 = (viewHolder3) convertView.getTag(); 128 Log.e("convertView !!!!!!= ", "NULL TYPE_3"); 129 break; 130 } 131 } 132 133 //設置資源 134 switch(type) 135 { 136 case TYPE_1: 137 holder1.textView.setText(Integer.toString(position)); 138 holder1.checkBox.setChecked(true); 139 break; 140 case TYPE_2: 141 holder2.textView.setText(Integer.toString(position)); 142 break; 143 case TYPE_3: 144 holder3.textView.setText(Integer.toString(position)); 145 holder3.imageView.setBackgroundResource(R.drawable.icon); 146 break; 147 } 148 149 150 return convertView; 151 } 152 153 }