我们知道,在微信或者QQ聊天的时候,会出现至少两种布局,即收到的消息和自己发送的消息,这种效果可以用listView来实现。类似于下面这样的界面。

主要在Adapter的getView()里面下笔。

  1. package com.example.chatting.chatting.adapter;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10.  
  11. import com.example.chatting.chatting.R;
  12. import com.example.chatting.chatting.bean.Chat;
  13.  
  14. import org.w3c.dom.Text;
  15.  
  16. import java.util.List;
  17.  
  18. /**
  19. * Created by Administrator on 2017/11/9.
  20. */
  21. public class ChattingAdapter extends BaseAdapter{
  22.  
  23. private List<Chat> list;
  24. private LayoutInflater inflater;
  25. private Context mContext;
  26. // private Drawable mDefaultHeadImage;
  27.  
  28. public ChattingAdapter(List<Chat> list, Context context){
  29. this.list = list;
  30. inflater = LayoutInflater.from(context);
  31. mContext = context;
  32. // mDefaultHeadImage = mContext.getResources().getDrawable(R.drawable.image_head);
  33. }
  34.  
  35. @Override
  36. public int getCount() {
  37. return list.size();
  38. }
  39.  
  40. @Override
  41. public Object getItem(int position) {
  42. return list.get(position);
  43. }
  44.  
  45. @Override
  46. public long getItemId(int position) {
  47. return position;
  48. }
  49. @Override
  50. public int getItemViewType(int position) {
  51. return list.get(position).getType();
  52. }
  53.  
  54. //一定得重写该方法,否则只会出现一种布局
  55. @Override
  56. public int getViewTypeCount() {
  57. // TODO Auto-generated method stub
  58. return 2;
  59. }
  60.  
  61. @Override
  62. public View getView(int position, View convertView, ViewGroup parent) {
  63. Chat chat = (Chat) getItem(position);
  64. int type = getItemViewType(position);
  65. ViewHolder viewHolder;
  66. if(convertView == null){
  67. if(type == 0) { // 我说的消息
  68. System.out.println("****type0");
  69. viewHolder = new ViewHolder();
  70. convertView = inflater.inflate(R.layout.list_chat_out, null);
  71. viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name_out);
  72. viewHolder.tvMsg = (TextView)convertView.findViewById(R.id.tv_msg_out);
  73. viewHolder.headImage = (ImageView)convertView.findViewById(R.id.image_head_out);
  74. }else{
  75. System.out.println("****type1");
  76. viewHolder = new ViewHolder();
  77. convertView = inflater.inflate(R.layout.list_chat_in, null);
  78. viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name_in);
  79. viewHolder.tvMsg = (TextView)convertView.findViewById(R.id.tv_msg_in);
  80. viewHolder.headImage = (ImageView)convertView.findViewById(R.id.image_head_in);
  81. }
  82. convertView.setTag(viewHolder);
  83. }else{
  84. viewHolder = (ViewHolder)convertView.getTag();
  85. }
  86.  
  87. viewHolder.tvName.setText(chat.getNickName());
  88. viewHolder.tvMsg.setText(chat.getMessage());
  89.  
  90. return convertView;
  91. }
  92.  
  93. private class ViewHolder{
  94. public TextView tvName, tvMsg;
  95. public ImageView headImage;
  96. }
  97.  
  98. }

通过

@Override

public int getItemViewType(int position) {

  1. return list.get(position).getType();
  2. }
  3.  
  4. 来决定实例化哪个布局,
  5.  
  6. 通过
    @Override
  1. public int getViewTypeCount() {
  2. // TODO Auto-generated method stub
  3. return 2;
  4. }
    来决定布局的类型个数。
  5. chat为封装聊天内容的java类:
  1. package com.example.chatting.chatting.bean;
  2.  
  3. import java.io.Serializable;
  4.  
  5. /**
  6. * Created by Administrator on 2017/11/9.
  7. */
  8. public class Chat implements Serializable{
  9. private String message;
  10. private int type;
  11. private String NickName;
  12. private String imgURL;
  13.  
  14. public String getMessage() {
  15. return message;
  16. }
  17.  
  18. public void setMessage(String message) {
  19. this.message = message;
  20. }
  21.  
  22. public int getType() {
  23. return type;
  24. }
  25.  
  26. public void setType(int type) {
  27. this.type = type;
  28. }
  29.  
  30. public String getNickName() {
  31. return NickName;
  32. }
  33.  
  34. public void setNickName(String nickName) {
  35. NickName = nickName;
  36. }
  37.  
  38. public String getImgURL() {
  39. return imgURL;
  40. }
  41.  
  42. public void setImgURL(String imgURL) {
  43. this.imgURL = imgURL;
  44. }
  45. }
  1.  
  1.  接下来是两个布局的代码:
    1
  1. list_chat_out:发送消息的布局
  1.  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:minHeight="80dp" >
  6.  
  7. <ImageView
  8. android:id="@+id/image_head_out"
  9. android:layout_width="45dp"
  10. android:layout_height="45dp"
  11. android:src="@mipmap/me_press"
  12. android:layout_alignParentRight="true"/>
  13.  
  14. <TextView
  15. android:id="@+id/tv_name_out"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_toLeftOf="@id/image_head_out"
  19. android:layout_alignTop="@id/image_head_out"
  20. android:layout_marginRight="5dp"
  21. android:textColor="@color/colorTheme"
  22. android:textSize="14sp"
  23. android:text="name"/>
  24.  
  25. <LinearLayout
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:minHeight="40dp"
  29. android:layout_toLeftOf="@id/image_head_out"
  30. android:layout_below="@id/tv_name_out"
  31. android:layout_marginRight="10dp"
  32. android:background="@color/colorTheme"
  33. android:gravity="center_vertical">
  34. <TextView
  35. android:id="@+id/tv_msg_out"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginLeft="15dp"
  39. android:layout_marginRight="15dp"
  40. android:textColor="@color/colorBlack"
  41. android:textSize="18sp"
  42. android:text="content"/>
  43. </LinearLayout>
  44.  
  45. </RelativeLayout>
  1. 2list_chat_in接收消息的布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:minHeight="80dp" >
  6.  
  7. <ImageView
  8. android:id="@+id/image_head_in"
  9. android:layout_width="45dp"
  10. android:layout_height="45dp"
  11. android:src="@mipmap/me_press"
  12. />
  13.  
  14. <TextView
  15. android:id="@+id/tv_name_in"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_toRightOf="@id/image_head_in"
  19. android:layout_alignTop="@id/image_head_in"
  20. android:layout_marginLeft="5dp"
  21. android:textColor="@color/colorTheme"
  22. android:textSize="14sp"
  23. android:text="name"/>
  24.  
  25. <LinearLayout
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:minHeight="40dp"
  29. android:layout_toRightOf="@id/image_head_in"
  30. android:layout_below="@id/tv_name_in"
  31. android:layout_marginLeft="10dp"
  32. android:background="@color/colorGray"
  33. android:gravity="center_vertical">
  34. <TextView
  35. android:id="@+id/tv_msg_in"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginRight="15dp"
  39. android:layout_marginLeft="15dp"
  40. android:textColor="@color/colorBlack"
  41. android:textSize="18sp"
  42. android:text="content"/>
  43. </LinearLayout>
  44.  
  45. </RelativeLayout>
  1.  
  1.  

聊天ListView的更多相关文章

  1. Android群英传笔记——第四章:ListView使用技巧

    Android群英传笔记--第四章:ListView使用技巧 最近也是比较迷茫,但是有一点点还是要坚持的,就是学习了,最近离职了,今天也是继续温习第四章ListView,也拖了其实也挺久的了,list ...

  2. Android群英传知识点回顾——第四章:ListView常用优化技巧

    4.1 ListView常用优化技巧 4.1.1 使用ViewHolder模式提高效率 4.1.2 设置项目间分割线 4.1.3 隐藏ListView的滚动条 4.1.4 取消ListView的Ite ...

  3. Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!

    Android群英传笔记--摘要,概述,新的出发点,温故而知新,可以为师矣! 当工作的越久,就越感到力不从心了,基础和理解才是最重要的,所以买了两本书,医生的<Android群英传>和主席 ...

  4. 自定义一个ListView实现聊天界面

    摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...

  5. ListView数据更新后,自动滚动到底部(聊天时常用)| Listview Scroll to the end of the list after updating the list

    转:http://www.cnblogs.com/bjshsqlt/p/3311830.html If you would like to after you have updated by list ...

  6. ListView:聊天界面

    一.最终成型图 二.主界面xml布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  7. Android—简单的仿QQ聊天界面

    最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,):

  8. xamarin android ListView手动分组

    xamarin的listview控件其实自带有分组方法,关于xamarin listview的自带分组方法请自行参考官方文档,我这里只写自己写的分组方法.xamarin自带的分组好是好,功能多,但是加 ...

  9. xamarin优化listView.ScrollTo

    在xamarinz中关于listview的滚动,我这里有点小优化,我做了一个类似QQ的聊天页面,上面是一个listview,下面时一个editText,当在手机上使用时,发现在android平台下,如 ...

随机推荐

  1. Scala - 快速学习03 - 基础语法

    1- 变量 变量 mutable variable 在程序运行过程中其值可能发生改变的量 关键词var定义变量,定义时直接进行求值 常量 immutable variable 在程序运行过程中其值不会 ...

  2. 基于vue-cli3.0构建功能完善的移动端架子,主要功能包括

    webpack 打包扩展 css:sass支持.normalize.css._mixin.scss._variables.scss vw.rem布局 跨域设置 eslint设置 cdn引入 路由设计. ...

  3. 十大经典排序算法(python实现)(原创)

    个人最喜欢的排序方法是非比较类的计数排序,简单粗暴.专治花里胡哨!!! 使用场景: 1,空间复杂度 越低越好.n值较大: 堆排序 O(nlog2n) O(1) 2,无空间复杂度要求.n值较大: 桶排序 ...

  4. [Jenkins]Jenkins构建时提示java.io.IOException: No space left on device

    突然发现Jenkins的Job全部都停了,打开Jenkins发现所有的slave机器,均提示: 点开Dead(!),提示Thread has died,如下图: 看图好像说是Jenkins所在的服务器 ...

  5. 小程序this.setData

    data: { isChecked: [ { key: true },{ key: true },{ key: true} ]} 如上,如果我想动态修改isChecked里面指定某个下标的值怎么办? ...

  6. docker - 容器里安装redis

    在docker中安装redis 使用命令行安装redis 下载并解压 wget http://download.redis.io/releases/redis-3.2.6.tar.gz tar -xv ...

  7. 通过keras例子理解LSTM 循环神经网络(RNN)

    博文的翻译和实践: Understanding Stateful LSTM Recurrent Neural Networks in Python with Keras 正文 一个强大而流行的循环神经 ...

  8. Oracle数据库over函数的使用

    转自:  https://blog.csdn.net/a1065423444/article/details/75635611 over()函数写法over(partition by expr2 or ...

  9. 分布式系统监视zabbix讲解五之web监控--技术流ken

    Web 监控 概况 你可以使用 Zabbix 检查几个网站可用性方面. 如果要使用 Web 检测功能,必须在 编译Zabbix 的时候加入 cURL(libcurl) 的支持. 要使用 Web 监控, ...

  10. 杭电ACM2000--ASCII码排序

    ASCII码排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...