黑马程序员技术交流社区

标题: 【源码】实现微信对话列 [打印本页]

作者: 枫儿    时间: 2013-12-24 17:03
标题: 【源码】实现微信对话列
这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信、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下载
下载地址:回复可见
[hide=d999999]http://pan.baidu.com/s/1nt16TqL[/hide]


作者: 李长波    时间: 2014-2-19 23:06
看看什么东西!
作者: 茗记    时间: 2014-2-27 23:53
:(:(:(:D:o:o
作者: 赵卓辉    时间: 2014-5-29 03:50
看看,学习学学习
作者: 973999644    时间: 2014-6-15 22:54
对方答复的
作者: doctorsoft    时间: 2015-3-6 13:02
谢谢.学习
作者: a410709560    时间: 2015-3-6 21:47
感谢分享!!!
作者: liuxwdz    时间: 2015-3-10 14:26
大神啊楼主
作者: hudi    时间: 2015-7-27 11:46
大神啊楼主
作者: 柠檬旋风腿    时间: 2015-7-30 22:08
好深奥啊
作者: 漠然心凉    时间: 2015-8-3 13:12
学习学习
作者: Alone_G4ykq    时间: 2015-11-5 21:05
bjfdfkrfdagrfgdf
作者: Alone_G4ykq    时间: 2015-11-5 21:07
32ewer3weww
作者: ztwdty    时间: 2015-12-28 23:18
学习大神
作者: zx942649    时间: 2016-3-28 09:51
98765432112346789*/
作者: liudan    时间: 2016-5-10 13:18
qswwwwwwwwwwwwwwwwww
作者: yanwenyong    时间: 2016-5-11 09:51
学习学习
作者: Tralse    时间: 2016-8-22 00:24
法国代购的非官方大哥
作者: Tralse    时间: 2016-8-22 00:28
看看能学到什么
作者: baby14    时间: 2019-7-9 07:41
多谢分享




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2