使用ListView实现汽泡短信聊天

小魏原创,欢迎转载~
转载请注明出处:http://blog.csdn.net/xiaowei_cqu/article/details/7045543

如前文http://blog.csdn.net/xiaowei_cqu/article/details/7045497

我们进行了SimpleAdapter适配器初次尝试,那么离实现我们最终想要的效果也不远啦,只要仿照chata的布局,再编写第二位聊天人(“路人甲”)的布局chatb——只要让他靠右显示就行~。

但是这样我们每次都要很麻烦的定义一遍SimpleAdapter,为了“偷懒”,我们直接来编写自己的Adapter,这样每次定义就方便多了。

先附上最终的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public void onCreate(Bundle savedInstanceState) {  
  2.             super.onCreate(savedInstanceState);  
  3.             setContentView(R.layout.main);  
  4.             chatlist = (ListView) findViewById(R.id.chatlist);  
  5.             list = new ArrayList<ChatEntity>();  
  6.             ChatEntity chat1=new ChatEntity("小魏","嗨~",R.layout.chata);  
  7.             list.add(chat1);  
  8.             ChatEntity chat2=new ChatEntity("路人甲","你好!",R.layout.chatb);  
  9.             list.add(chat2);  
  10.             ChatEntity chat3=new ChatEntity("小魏","我是小魏~",R.layout.chata);  
  11.             list.add(chat3);  
  12.               
  13.             chatlist.setAdapter(new ChatAdapter(TryChatPop2Activity.this,list));  
  14. }  

如上代码,在setAdapter时使用了自己的ChatAdapter,以下是类文件代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ChatAdapter implements ListAdapter{  
  2.     private ArrayList<ChatEntity> list;  
  3.     private Context ctx;  
  4.   
  5.     public ChatAdapter(Context context ,ArrayList<ChatEntity> list) {  
  6.         ctx = context;  
  7.         this.list = list;  
  8.     }  
  9.       
  10.     public boolean areAllItemsEnabled() {  
  11.         return false;  
  12.     }  
  13.     public boolean isEnabled(int arg0) {  
  14.         return false;  
  15.     }  
  16.     public int getCount() {  
  17.         return list.size();  
  18.     }  
  19.     public Object getItem(int position) {  
  20.         return list.get(position);  
  21.     }  
  22.     public long getItemId(int position) {  
  23.         return position;  
  24.     }  
  25.     public int getItemViewType(int position) {  
  26.         return position;  
  27.     }  
  28.     public View getView(int position, View convertView, ViewGroup parent) {  
  29.         ChatEntity entity = list.get(position);  
  30.         int itemLayout = entity.getLayoutID();  
  31.           
  32.         LinearLayout layout = new LinearLayout(ctx);  
  33.         LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  34.         vi.inflate(itemLayout, layout,true);  
  35.           
  36.         TextView txvName = (TextView) layout.findViewById(R.id.txvName);  
  37.         txvName.setText(entity.getName());  
  38.           
  39.         TextView txvText = (TextView) layout.findViewById(R.id.txvInfo);  
  40.         txvText.setText(entity.getInfo());  
  41.         return layout;  
  42.     }  
  43.     public int getViewTypeCount() {  
  44.         return list.size();  
  45.     }  
  46.     public boolean hasStableIds() {  
  47.         return false;  
  48.     }  
  49.     public boolean isEmpty() {  
  50.         return false;  
  51.     }  
  52.     public void registerDataSetObserver(DataSetObserver observer) {  
  53.     }  
  54.     public void unregisterDataSetObserver(DataSetObserver observer) {  
  55.     }  
  56.   
  57. }  

ChatAdapterd的类实现了ListAdapter的接口,并通过ChatEntity中的内容设置了定义布局中聊天对象名字txvName及聊天内容txvInfo的内容,当然你肯定能明白ChatEntity就是存放聊天信息等内容的实体类。

这里我们可以这样写,就是因为ListAdapter的接口是绑定Data和ListView的适配器,实际上我们常用的ArryaAdapter、SimpleAdapter、CursorAdapter就是他的子类。

关系如下:

这样再看代码,甚至再回头看SimpleAdapter就感觉好理解多了,其他内容不细说了,具体参照源码:http://download.csdn.net/detail/xiaowei_cqu/3886321

再上一遍效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值