筆記,listview視圖總結 1 public class HomeEduMoreAdapter extends BaseAdapter { 2 3 private final String TAG = "HomeEduMoreAdapter"; 4 5 private static final
筆記,listview視圖總結
1 public class HomeEduMoreAdapter extends BaseAdapter { 2 3 private final String TAG = "HomeEduMoreAdapter"; 4 5 private static final int TYPE_COMMON = 0; 6 private static final int TYPE_BL = 1; 7 private static final int TYPE_NONE = 2; 8 private static final int TYPE_THREE = 3; 9 private static final int TYPE_QUESTION = 4; 10 private int TYPE_COUNT = 5; 11 12 private LayoutInflater inflater; 13 private List<HomeEdus> list; 14 private Context context; 15 private DisplayImageOptions options; 16 private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); 17 18 public HomeEduMoreAdapter(Context context, List<HomeEdus> objects) { 19 this.context = context; 20 // groupUserDao = new GroupUserDao(context); 21 inflater = LayoutInflater.from(context); 22 list = objects; 23 options = new DisplayImageOptions.Builder().cacheInMemory(true) 24 .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 25 .bitmapConfig(Bitmap.Config.RGB_565).cacheOnDisk(true) 26 .showImageOnLoading(R.drawable.icon_default)// 設置圖片在下載期間顯示的圖片 27 .showImageForEmptyUri(R.drawable.icon_default)// 設置圖片Uri為空或是錯誤的時候顯示的圖片 28 .showImageOnFail(R.drawable.icon_default)// 設置圖片載入/解碼過程中錯誤時候顯示的圖片 29 .considerExifParams(true).build(); 30 } 31 32 @Override 33 public int getCount() { 34 // TODO Auto-generated method stub 35 return list.size(); 36 } 37 38 @Override 39 public int getViewTypeCount() { 40 // TODO Auto-generated method stub 41 return TYPE_COUNT; 42 } 43 44 @Override 45 public HomeEdus getItem(int position) { 46 // TODO Auto-generated method stub 47 return list.get(position); 48 } 49 50 @Override 51 public long getItemId(int postion) { 52 // TODO Auto-generated method stub 53 return postion; 54 } 55 56 @Override 57 public int getItemViewType(int position) { 58 // TODO Auto-generated method stub 59 HomeEdus edus = getItem(position); 60 61 String type = edus.getType();// 菜單類型 62 int result = TYPE_COMMON; 63 switch (type) { 64 case "hx": 65 result = TYPE_COMMON; 66 break; 67 case "wd": 68 if (edus.getPic1() == null || edus.getPic1().equals("")) { 69 result = TYPE_QUESTION; 70 }else { 71 result = TYPE_COMMON; 72 } 73 74 break; 75 case "jt": 76 result = TYPE_COMMON; 77 break; 78 case "bl": 79 result = TYPE_BL; 80 break; 81 default: 82 String jx = edus.getType();// 精選內容類型 83 switch (jx) { 84 default: 85 Integer imgNum = Integer.parseInt(edus.getImgNum()); 86 if (imgNum != null && imgNum > 0 && imgNum < 3) { 87 result = TYPE_COMMON; 88 } else if (imgNum != null && imgNum >= 3) { 89 result = TYPE_THREE; 90 } else { 91 result = TYPE_NONE; 92 } 93 94 break; 95 } 96 97 break; 98 } 99 return result; 100 } 101 102 @Override 103 public View getView(int position, View convertView, ViewGroup parent) { 104 // TODO Auto-generated method stub 105 final HomeEdus model = getItem(position); 106 int type = getItemViewType(position); 107 final ViewHolder holder; 108 109 if (convertView == null) { 110 holder = new ViewHolder(); 111 WindowManager manager = (WindowManager) context 112 .getSystemService(Context.WINDOW_SERVICE); 113 int width = manager.getDefaultDisplay().getWidth(); 114 int height = manager.getDefaultDisplay().getWidth() / 2; 115 switch (type) { 116 case TYPE_COMMON: 117 convertView = inflater.inflate(R.layout.item_home_edus, null); 118 holder.tv_content = (QSTextView) convertView 119 .findViewById(R.id.tv_content); 120 holder.tv_content.setTextColor(Color.GRAY); 121 holder.tv_title = (QSTextView) convertView 122 .findViewById(R.id.tv_title); 123 holder.tv_name = (TextView) convertView 124 .findViewById(R.id.tv_name); 125 // holder.img_head = (CircleImageView) convertView 126 // .findViewById(R.id.img_head); 127 holder.img_content = (ImageView) convertView 128 .findViewById(R.id.img_content); 129 holder.tv_read = (TextView) convertView 130 .findViewById(R.id.tv_read); 131 holder.tv_comment = (TextView) convertView 132 .findViewById(R.id.tv_comment); 133 holder.tv_thumbupNum = (TextView) convertView 134 .findViewById(R.id.tv_thumbupNum); 135 break; 136 case TYPE_QUESTION: 137 convertView = inflater.inflate(R.layout.item_home_question, null); 138 holder.tv_content = (QSTextView) convertView 139 .findViewById(R.id.tv_content); 140 holder.tv_content.setTextColor(Color.GRAY); 141 holder.tv_title = (QSTextView) convertView 142 .findViewById(R.id.tv_title); 143 holder.tv_name = (TextView) convertView 144 .findViewById(R.id.tv_name); 145 holder.tv_read = (TextView) convertView 146 .findViewById(R.id.tv_read); 147 holder.tv_comment = (TextView) convertView 148 .findViewById(R.id.tv_comment); 149 holder.tv_thumbupNum = (TextView) convertView 150 .findViewById(R.id.tv_thumbupNum); 151 break; 152 case TYPE_BL: 153 case TYPE_NONE: 154 convertView = inflater.inflate(R.layout.item_home_case, null); 155 holder.tv_content = (QSTextView) convertView 156 .findViewById(R.id.tv_content); 157 holder.tv_content.setTextColor(Color.GRAY); 158 holder.tv_title = (QSTextView) convertView 159 .findViewById(R.id.tv_title); 160 holder.tv_name = (TextView) convertView 161 .findViewById(R.id.tv_name); 162 holder.img_content = (ImageView) convertView 163 .findViewById(R.id.img_content); 164 165 LayoutParams para1 = holder.img_content.getLayoutParams(); 166 para1.height = height; 167 para1.width = width; 168 holder.img_content.setLayoutParams(para1); 169 170 holder.tv_read = (TextView) convertView 171 .findViewById(R.id.tv_read); 172 holder.tv_comment = (TextView) convertView 173 .findViewById(R.id.tv_comment); 174 holder.tv_thumbupNum = (TextView) convertView 175 .findViewById(R.id.tv_thumbupNum); 176 break; 177 178 case TYPE_THREE: 179 convertView = inflater.inflate(R.layout.item_home_three, null); 180 holder.tv_content = (QSTextView) convertView 181 .findViewById(R.id.tv_content); 182 holder.tv_content.setTextColor(Color.GRAY); 183 holder.tv_title = (QSTextView) convertView 184 .findViewById(R.id.tv_title); 185 holder.tv_name = (TextView) convertView 186 .findViewById(R.id.tv_name); 187 holder.img_content1 = (ImageView) convertView 188 .findViewById(R.id.img_content1); 189 holder.img_content2 = (ImageView) convertView 190 .findViewById(R.id.img_content2); 191 holder.img_content3 = (ImageView) convertView 192 .findViewById(R.id.img_content3); 193 int w = width / 3; 194 LayoutParams p = holder.img_content1.getLayoutParams(); 195 p.height = (int)(w*0.7f); 196 p.width = w; 197 holder.img_content1.setLayoutParams(p); 198 199 p = holder.img_content2.getLayoutParams(); 200 p.height = (int)(w*0.7f); 201 p.width = w; 202 holder.img_content2.setLayoutParams(p); 203 204 p = holder.img_content3.getLayoutParams(); 205 p.height = (int)(w*0.7f); 206 p.width = w; 207 holder.img_content3.setLayoutParams(p); 208 209 holder.tv_read = (TextView) convertView 210 .findViewById(R.id.tv_read); 211 holder.tv_comment = (TextView) convertView 212 .findViewById(R.id.tv_comment); 213 holder.tv_thumbupNum = (TextView) convertView 214 .findViewById(R.id.tv_thumbupNum); 215 break; 216 217 } 218 219 convertView.setTag(holder); 220 } else { 221 holder = (ViewHolder) convertView.getTag(); 222 } 223 224 switch (type) { 225 case TYPE_COMMON: 226 case TYPE_BL: 227 setCommon(holder, model); 228 break; 229 case TYPE_QUESTION: 230 setQuestion(holder,model); 231 break; 232 case TYPE_NONE: 233 setCase(holder, model); 234 break; 235 case TYPE_THREE: 236 setThree(holder, model); 237 break; 238 } 239 240 return convertView; 241 } 242 243 private void setCommon(ViewHolder holder, HomeEdus entity) { 244 String content = entity.getDesc(); 245 int clength = content.length(); 246 if (clength > 65) { 247 content = content.substring(0, 65) + "..."; 248 } 249 holder.tv_content.setMText(content); 250 251 String title = entity.getTitle(); 252 int tlength = title.length(); 253 if (tlength > 45) { 254 title = title.substring(0, 45) + "..."; 255 } 256 holder.tv_title.setMText(title); 257 // int height = holder.tv_title.getHeight(); 258 holder.tv_thumbupNum.setText(entity.getThumbupNum()); 259 holder.tv_read.setText(entity.getReadNum()); 260 holder.tv_comment.setText(entity.getCommentNum()); 261 262 holder.tv_name.setText(entity.getSourceName()); 263 // if (entity.getHead_img() != null && !entity.getHead_img().equals("")) 264 // { 265 // ImageLoader.getInstance().displayImage(entity.getHead_img().trim(), 266 // holder.img_head, options, animateFirstListener); 267 // } else { 268 // holder.img_head.setImageResource(R.drawable.pic_head_none_login); 269 // } 270 if (entity.getPic1() != null 271 && !entity.getPic1().equals("")) { 272 holder.img_content.setBackground(null); 273 ImageLoader.getInstance().displayImage( 274 entity.getPic1().trim(), holder.img_content, 275 options, animateFirstListener); 276 } 277 } 278 279 private void setCase(ViewHolder holder, HomeEdus entity) { 280 String content = entity.getDesc(); 281 int clength = content.length(); 282 if (clength > 65) { 283 content = content.substring(0, 65) + "..."; 284 } 285 holder.tv_content.setMText(content); 286 287 String title = entity.getTitle(); 288 int tlength = title.length(); 289 if (tlength > 45) { 290 title = title.substring(0, 45) + "..."; 291 } 292 holder.tv_title.setMText(title); 293 // int height = holder.tv_title.getHeight(); 294 holder.tv_thumbupNum.setText(entity.getThumbupNum()); 295 holder.tv_read.setText(entity.getReadNum()); 296 holder.tv_comment.setText(entity.getCommentNum()); 297 298 holder.tv_name.setText(entity.getSourceName()); 299 if (entity.getPic1() != null 300 && !entity.getPic1().equals("")) { 301 holder.img_content.setBackground(null); 302 ImageLoader.getInstance().displayImage( 303 entity.getPic1().trim(), holder.img_content, 304 options, animateFirstListener); 305 } 306 } 307 308 private void setThree(ViewHolder holder, HomeEdus entity) { 309 String content = entity.getDesc(); 310 int clength = content.length(); 311 if (clength > 65) { 312 content = content.substring(0, 65) + "..."; 313 } 314 holder.tv_content.setMText(content); 315 316 String title = entity.getTitle(); 317 int tlength = title.length(); 318 if (tlength > 45) { 319 title = title.substring(0, 45) + "..."; 320 } 321 holder.tv_title.setMText(title); 322 // int height = holder.tv_title.getHeight(); 323 holder.tv_thumbupNum.setText(entity.getThumbupNum()); 324 holder.tv_read.setText(entity.getReadNum()); 325 holder.tv_comment.setText(entity.getCommentNum()); 326 327 holder.tv_name.setText(entity.getSourceName()); 328 329 holder.img_content1.setImageResource(R.drawable.icon_default); 330 holder.img_content2.setImageResource(R.drawable.icon_default); 331 holder.img_content3.setImageResource(R.drawable.icon_default); 332 if (entity.getPic1() != null 333 && !entity.getPic1().equals("")) { 334 holder.img_content1.setBackground(null); 335 ImageLoader.getInstance().displayImage( 336 entity.getPic1().trim(), holder.img_content1, 337 options, animateFirstListener); 338 } 339 if (entity.getPic2() != null 340 && !entity.getPic2().equals("")) { 341 holder.img_content1.setBackground(null); 342 ImageLoader.getInstance().displayImage( 343 entity.getPic1().trim(), holder.img_content2, 344 options, animateFirstListener); 345 } 346 if (entity.getPic3() != null 347 && !entity.getPic3().equals("")) { 348 holder.img_content1.setBackground(null); 349 ImageLoader.getInstance().displayImage( 350 entity.getPic1().trim(), holder.img_content3, 351 options, animateFirstListener); 352 } 353 } 354 355 private void setQuestion(ViewHolder holder, HomeEdus entity) { 356 String content = entity.getDesc(); 357 int clength = content.length(); 358 if (clength > 65) { 359 content = content.substring(0, 65) + "..."; 360 } 361 holder.tv_content.setMText(content); 362 363 String title = entity.getTitle(); 364 int tlength = title.length(); 365 if (tlength > 45) { 366 title = title.substring(0, 45) + "..."; 367 } 368 holder.tv_title.setMText(title); 369 // int height = holder.tv_title.getHeight(); 370 holder.tv_thumbupNum.setText(entity.getThumbupNum()); 371 holder.tv_read.setText(entity.getReadNum()); 372 holder.tv_comment.setText(entity.getCommentNum()); 373 374 holder.tv_name.setText(entity.getSourceName()); 375 376 } 377 378 public void setList(List<HomeEdus> list) { 379 this.list.clear(); 380 this.list.addAll(list); 381 notifyDataSetChanged(); 382 } 383 384 static class ViewHolder { 385 public QSTextView tv_title; 386 public QSTextView tv_content; 387 public ImageView img_content; 388 public TextView tv_name; 389 390 public TextView tv_read; 391 public TextView tv_comment; 392 public TextView tv_thumbupNum; 393 394 public ImageView img_content1; 395 public ImageView img_content2; 396 public ImageView img_content3; 397 } 398 399 private static class AnimateFirstDisplayListener extends 400 SimpleImageLoadingListener { 401 402 static final List<String> displayedImages = Collections 403 .synchronizedList(new LinkedList<String>()); 404 405 @Override 406 public void onLoadingComplete(String imageUri, View view, 407 Bitmap loadedImage) { 408 if (loadedImage != null) { 409 ImageView imageView = (ImageView) view; 410 boolean firstDisplay = !displayedImages.contains(imageUri); 411 if (firstDisplay) { 412 FadeInBitmapDisplayer.animate(imageView, 500); 413 displayedImages.add(imageUri); 414 } 415 } 416 } 417 } 418 419 }View Code