A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 枫儿 金牌黑马   /  2013-12-24 17:03  /  3409 人查看  /  19 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信、whatsapp、易信、米聊等。我们这篇文章也权当为回忆,形成简单的笔记。这篇文章参考了2009年Google IO中的《TurboChargeYourUI-How to make your AndroidUI fast and efficient》和2010年Google IO中的《The World of List View》。像2009年Google IO的资料还是很前沿的,那会android开发资料很少,最重要的就是参考google发布的各种资料。


    在《TurboChargeYourUI-How to make your AndroidUI fast and efficient》介绍了怎样提高listview的性能,优化了listview的加载速度。这里的item使用的是单一布局,能够实现view的重用和回收,那么多种布局文件的怎么办呢,如果再使用上面的方法,view的重用会出现问题,Android使用的BaseAdapter提供了解决多种布局文件的重用方法。

1)重写 getViewTypeCount() – 该方法返回多少个不同的布局

2)重写 getItemViewType(int) – 根据position返回相应的Item

  1. /**
  2. * 比原来的多了getItemViewType和getViewTypeCount这两个方法,
  3. *  
  4. * */  
  5. public class ChatAdapter extends BaseAdapter {  
  6.   
  7.     public static final String KEY = "key";  
  8.     public static final String VALUE = "value";  
  9.   
  10.     public static final int VALUE_TIME_TIP = 0;// 7种不同的布局  
  11.     public static final int VALUE_LEFT_TEXT = 1;  
  12.     public static final int VALUE_LEFT_IMAGE = 2;  
  13.     public static final int VALUE_LEFT_AUDIO = 3;  
  14.     public static final int VALUE_RIGHT_TEXT = 4;  
  15.     public static final int VALUE_RIGHT_IMAGE = 5;  
  16.     public static final int VALUE_RIGHT_AUDIO = 6;  
  17.     private LayoutInflater mInflater;  
  18.   
  19.     private List<Message> myList;  
  20.   
  21.     public ChatAdapter(Context context, List<Message> myList) {  
  22.         this.myList = myList;  
  23.   
  24.         mInflater = (LayoutInflater) context  
  25.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  26.     }  
  27.       
  28.     public int getCount() {  
  29.         return myList.size();  
  30.     }  
  31.       
  32.     public Object getItem(int arg0) {  
  33.         return myList.get(arg0);  
  34.     }  
  35.       
  36.     public long getItemId(int arg0) {  
  37.         return arg0;  
  38.     }  
  39.       
  40.     public View getView(int position, View convertView, ViewGroup arg2) {  
  41.   
  42.         Message msg = myList.get(position);  
  43.         int type = getItemViewType(position);  
  44.         ViewHolderTime holderTime = null;  
  45.         ViewHolderRightText holderRightText = null;  
  46.         ViewHolderRightImg holderRightImg = null;  
  47.         ViewHolderRightAudio holderRightAudio = null;  
  48.         ViewHolderLeftText holderLeftText = null;  
  49.         ViewHolderLeftImg holderLeftImg = null;  
  50.         ViewHolderLeftAudio holderLeftAudio = null;  
  51.          
  52.         if (convertView == null) {  
  53.             switch (type) {  
  54.             case VALUE_TIME_TIP:  
  55.                 holderTime = new ViewHolderTime();  
  56.                 convertView = mInflater.inflate(R.layout.list_item_time_tip,  
  57.                         null);  
  58.                 holderTime.tvTimeTip = (TextView) convertView  
  59.                         .findViewById(R.id.tv_time_tip);  
  60.                 holderTime.tvTimeTip.setText(msg.getValue());  
  61.                 convertView.setTag(holderTime);  
  62.                 break;  
  63.             // 左边  
  64.             case VALUE_LEFT_TEXT:  
  65.                 holderLeftText = new ViewHolderLeftText();  
  66.                 convertView = mInflater.inflate(R.layout.list_item_left_text,  
  67.                         null);  
  68.                 holderLeftText.ivLeftIcon = (ImageView) convertView  
  69.                         .findViewById(R.id.iv_icon);  
  70.                 holderLeftText.btnLeftText = (Button) convertView  
  71.                         .findViewById(R.id.btn_left_text);  
  72.                 holderLeftText.btnLeftText.setText(msg.getValue());  
  73.                 convertView.setTag(holderLeftText);  
  74.                 break;  
  75.   
  76.             case VALUE_LEFT_IMAGE:  
  77.                 holderLeftImg = new ViewHolderLeftImg();  
  78.                 convertView = mInflater.inflate(R.layout.list_item_left_iamge,  
  79.                         null);  
  80.                 holderLeftImg.ivLeftIcon = (ImageView) convertView  
  81.                         .findViewById(R.id.iv_icon);  
  82.                 holderLeftImg.ivLeftImage = (ImageView) convertView  
  83.                         .findViewById(R.id.iv_left_image);  
  84.                 holderLeftImg.ivLeftImage.setImageResource(R.drawable.test);  
  85.                 convertView.setTag(holderLeftImg);  
  86.                 break;  
  87.   
  88.             case VALUE_LEFT_AUDIO:  
  89.                 holderLeftAudio = new ViewHolderLeftAudio();  
  90.                 convertView = mInflater.inflate(R.layout.list_item_left_audio,  
  91.                         null);  
  92.                 holderLeftAudio.ivLeftIcon = (ImageView) convertView  
  93.                         .findViewById(R.id.iv_icon);  
  94.                 holderLeftAudio.btnLeftAudio = (Button) convertView  
  95.                         .findViewById(R.id.btn_left_audio);  
  96.                 holderLeftAudio.tvLeftAudioTime = (TextView) convertView  
  97.                         .findViewById(R.id.tv_left_audio_time);  
  98.                 holderLeftAudio.tvLeftAudioTime.setText(msg.getValue());  
  99.                 convertView.setTag(holderLeftAudio);  
  100.                 break;  
  101.             // 右边  
  102.             case VALUE_RIGHT_TEXT:  
  103.                 holderRightText= new ViewHolderRightText();  
  104.                 convertView = mInflater.inflate(R.layout.list_item_right_text,  
  105.                         null);  
  106.                 holderRightText.ivRightIcon = (ImageView) convertView  
  107.                         .findViewById(R.id.iv_icon);  
  108.                 holderRightText.btnRightText = (Button) convertView  
  109.                         .findViewById(R.id.btn_right_text);  
  110.                 holderRightText.btnRightText.setText(msg.getValue());  
  111.                 convertView.setTag(holderRightText);  
  112.                 break;  
  113.   
  114.             case VALUE_RIGHT_IMAGE:  
  115.                 holderRightImg= new ViewHolderRightImg();  
  116.                 convertView = mInflater.inflate(R.layout.list_item_right_iamge,  
  117.                         null);  
  118.                 holderRightImg.ivRightIcon = (ImageView) convertView  
  119.                         .findViewById(R.id.iv_icon);  
  120.                 holderRightImg.ivRightImage = (ImageView) convertView  
  121.                         .findViewById(R.id.iv_right_image);  
  122.                 holderRightImg.ivRightImage.setImageResource(R.drawable.test);  
  123.                 convertView.setTag(holderRightImg);  
  124.                 break;  
  125.   
  126.             case VALUE_RIGHT_AUDIO:  
  127.                 holderRightAudio=new ViewHolderRightAudio();  
  128.                 convertView = mInflater.inflate(R.layout.list_item_right_audio,  
  129.                         null);  
  130.                 holderRightAudio.ivRightIcon = (ImageView) convertView  
  131.                         .findViewById(R.id.iv_icon);  
  132.                 holderRightAudio.btnRightAudio = (Button) convertView  
  133.                         .findViewById(R.id.btn_right_audio);  
  134.                 holderRightAudio.tvRightAudioTime = (TextView) convertView  
  135.                         .findViewById(R.id.tv_right_audio_time);  
  136.                 holderRightAudio.tvRightAudioTime.setText(msg.getValue());  
  137.                 convertView.setTag(holderRightAudio);  
  138.                 break;  
  139.   
  140.             default:  
  141.                 break;  
  142.             }  
  143.               
  144.         } else {  
  145.             Log.d("baseAdapter", "Adapter_:"+(convertView == null) );  
  146.             switch (type) {  
  147.             case VALUE_TIME_TIP:  
  148.                 holderTime=(ViewHolderTime)convertView.getTag();  
  149.                 holderTime.tvTimeTip.setText(msg.getValue());  
  150.                 break;  

  151.   ..........
复制代码

分享两张微信、易信的图,你也可以做成这样子

               
Demo下载
下载地址:回复可见
游客,如果您要查看本帖隐藏内容请回复


19 个回复

倒序浏览
看看什么东西!
回复 使用道具 举报
:(:(:(:D:o:o
回复 使用道具 举报
看看,学习学学习
回复 使用道具 举报
对方答复的
回复 使用道具 举报
谢谢.学习
回复 使用道具 举报
感谢分享!!!
回复 使用道具 举报
大神啊楼主
回复 使用道具 举报
hudi 初级黑马 2015-7-27 11:46:01
9#
大神啊楼主
回复 使用道具 举报
好深奥啊
回复 使用道具 举报
学习学习
回复 使用道具 举报
bjfdfkrfdagrfgdf
回复 使用道具 举报
32ewer3weww
回复 使用道具 举报
ztwdty 初级黑马 2015-12-28 23:18:27
14#
学习大神
回复 使用道具 举报
98765432112346789*/
回复 使用道具 举报
qswwwwwwwwwwwwwwwwww
回复 使用道具 举报
学习学习
回复 使用道具 举报
法国代购的非官方大哥
回复 使用道具 举报
看看能学到什么
回复 使用道具 举报
多谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马