Activity:

 package com.zzw.qqchat;

 import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity {
private final int VIEW_TYPE = 0x001;
private final int MESSAGE = 0x002; private final int VIEW_TYPE_LEFT = -1;
private final int VIEW_TYPE_RIGHT = -2; private ArrayList<HashMap<Integer, Object>> items;
private MyAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ListView listView = (ListView) findViewById(R.id.list); items = new ArrayList<HashMap<Integer, Object>>(); addData(); adapter = new MyAdapter(this, -1);
listView.setAdapter(adapter); final EditText et = (EditText) findViewById(R.id.msgEditText); findViewById(R.id.msgSend).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
String msg = et.getText() + ""; HashMap<Integer, Object> map = new HashMap<Integer, Object>();
map.put(VIEW_TYPE, VIEW_TYPE_RIGHT);
map.put(MESSAGE, msg);
items.add(map);
// item数据变化时通知adapter刷新及时改变view
adapter.notifyDataSetChanged();
// 刷新后清空输入框
et.setText("");
// 输入框发送消息后将ListView滚动到底部
listView.setSelection(ListView.FOCUS_DOWN); }
});
} private void addData() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
HashMap<Integer, Object> map = new HashMap<Integer, Object>();
map.put(VIEW_TYPE, VIEW_TYPE_LEFT);
map.put(MESSAGE, "对方说的消息:" + i);
items.add(map);
} else {
HashMap<Integer, Object> map = new HashMap<Integer, Object>();
map.put(VIEW_TYPE, VIEW_TYPE_RIGHT);
map.put(MESSAGE, "我说的消息:" + i);
items.add(map);
}
}
} private class MyAdapter extends ArrayAdapter {
private LayoutInflater inflater; public MyAdapter(Context context, int resource) {
super(context, resource);
inflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return items.size();
} @Override
public String getItem(int position) { String message = items.get(position).get(MESSAGE) + "";
return message;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position);
String message = getItem(position); switch (type) {
case VIEW_TYPE_LEFT:
if (convertView == null) {
convertView = inflater.inflate(R.layout.left, null);
}
TextView textViewLeft = (TextView) convertView.findViewById(R.id.textView);
textViewLeft.setText(message);
break; case VIEW_TYPE_RIGHT:
if (convertView == null) {
convertView = inflater.inflate(R.layout.right, null);
}
TextView textViewRight = (TextView) convertView.findViewById(R.id.textView);
textViewRight.setText(message);
break;
} return convertView;
} @Override
public int getItemViewType(int position) {
HashMap map = items.get(position);
int type = (Integer) map.get(VIEW_TYPE);
return type;
} @Override
public int getViewTypeCount() {
return 2;
}
}
}

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/commentLinearLayout"
android:layout_alignParentTop="true"
android:divider="@android:color/transparent"
android:dividerHeight="15dip"
android:scrollbars="none" /> <LinearLayout
android:id="@+id/commentLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#e0e0e0"
android:orientation="horizontal" > <EditText
android:id="@+id/msgEditText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="8"
android:hint="发送消息" /> <Button
android:id="@+id/msgSend"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="发送" />
</LinearLayout> </RelativeLayout>

activity_main

left.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:singleLine="false"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageView"
android:background="#ff5252"
android:text="left" /> </RelativeLayout>

left

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imageView"
android:background="#2196f3"
android:singleLine="false"
android:text="right" /> </RelativeLayout>

right

QQl聊天消息的更多相关文章

  1. 即时通信系统中如何实现:聊天消息加密,让通信更安全? 【低调赠送:QQ高仿版GG 4.5 最新源码】

    加密重要的通信消息,是一个常见的需求.在一些政府部门的即时通信软件中(如税务系统),对聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新的GG 4.5中,增加了对聊天消息 ...

  2. spark结合 Openfire服务器,发送聊天消息

    1.下载OpenFire服务器,进行安装,参考http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html 2.程序运行客户端:下载客户端代 ...

  3. 微信技术分享:微信的海量IM聊天消息序列号生成实践(算法原理篇)

    1.点评 对于IM系统来说,如何做到IM聊天消息离线差异拉取(差异拉取是为了节省流量).消息多端同步.消息顺序保证等,是典型的IM技术难点. 就像即时通讯网整理的以下IM开发干货系列一样: <I ...

  4. 【转】编写微信聊天机器人4《聊天精灵WeChatGenius》:实时获取到微信聊天消息,hook数据库插入操作。

    接上篇,使用Xposed来hook微信,找到微信进程:https://blog.csdn.net/weixin_42127613/article/details/81839537 既然已经找到了微信进 ...

  5. 即时通信系统中实现聊天消息加密,让通信更安全【低调赠送:C#开源即时通讯系统(支持广域网)——GGTalk4.5 最新源码】

    在即时通讯系统(IM)中,加密重要的通信消息,是一个常见的需求.尤其在一些政府部门的即时通信软件中(如税务系统),对即时聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新 ...

  6. iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能

    类似微信聊天消息中的电话号码点击保存到通讯录中的功能,ABAddress的实现在iOS9中是不能正常使用的,点击完成后,手机会非常的卡,iOS9之后需要使用Contact新提供的方法来实现该功能.快捷 ...

  7. Smack 结合 Openfire服务器,建立IM通信,发送聊天消息

    在文章开始,请你了解和熟悉openfire方面的相关知识,这样对你理解下面代码以及下面代码的用途有很好的了解.同时,你可能需要安装一个简单的CS聊天工具,来测试你的代码是否成功的在openfire服务 ...

  8. 融云技术分享:解密融云IM产品的聊天消息ID生成策略

    本文来自融云技术团队原创分享,原文发布于“融云全球互联网通信云”公众号,原题<如何实现分布式场景下唯一 ID 生成?>,即时通讯网收录时有部分改动. 1.引言 对于IM应用来说,消息ID( ...

  9. reactnative实现qq聊天消息气泡拖拽消失效果

    前言(可跳过) 我在开发自己的APP时遇到了一个类似于qq聊天消息气泡拖拽消息的需求,因为在网上没有找到相关的组件,所以自己动手实现了一下 需求:对聊天消息气泡拖拽到一定长度松开时该气泡会消失(可自行 ...

随机推荐

  1. sql server 2008 System.Data.SqlClient.SqlException (0x80131904): 查询处理器未能为执行并行查询启动必要的线程资源 处理方法

    修改并行度: 修改了这个“最大并行度”,如果再没出现 cxpacket应该没问题了 参考资料:http://jingyan.baidu.com/article/5d6edee22daf8799eade ...

  2. (转).NET代码混淆实践

    今天突然对反编译.混淆感兴趣,在网上查了一些资料,文章大部分内容来源http://www.cnblogs.com/hsapphire/archive/2010/11/21/1883514.html. ...

  3. excel读取指定行

    任意单元格输入=OFFSET($A$1,(ROW(A1)-1)*20,0) 往下拉就出来了

  4. X230上安装Yosemite/Win7-黑苹果之路

    以前曾经在X230上安装了mavericks,但因为无线网卡问题最终作罢,现在换了SSD(128G).AR9285网卡,又冲刺了一把OSX,折腾了好几天,终于成了.特做记录如下: 首先,硬盘分区问题, ...

  5. LoadRunner界面分析(一)

    1.Virtual User Generator 2.新建脚本的方式 3.Task模式 4.Recording Options选项 5.Run-Time setting选项

  6. IIS7.0设置404错误页,返回500状态码

    一般在II6下,设置自定义404错误页时,只需要在错误页中选择自定义的页面,做自己的404页面即可.但是在IIS7.0及以上时,设置完404错误页后,会发现状态码返回的是500,并且可能会引起页面乱码 ...

  7. 【PL/SQL练习】函数

    1.必须返回一个值2.只能在表达式调用 SQL> create or replace function fun1 return number is v_sum_sal emp.sal%type; ...

  8. 学习练习 java编写西游记人物类

    package com.hanqi; public class XiYouJiRenWu { double height; String weapon; String name; void prine ...

  9. phpexcel 导入导出

    导出excel /** * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要 * 打开对应行的注释. * 如果使用 Excel5 ,输出的内容应该是GBK编码. */ //r ...

  10. 实现MySQL的Replication

    实现MySQL的Replication     实现MySQL的Replication在MySQL 3.23.15版本之后,MySQL提供了数据库复制的功能,可以实现两个数据库实时同步,增强了MySQ ...