QQl聊天消息

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聊天消息的更多相关文章
- 即时通信系统中如何实现:聊天消息加密,让通信更安全? 【低调赠送:QQ高仿版GG 4.5 最新源码】
加密重要的通信消息,是一个常见的需求.在一些政府部门的即时通信软件中(如税务系统),对聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新的GG 4.5中,增加了对聊天消息 ...
- spark结合 Openfire服务器,发送聊天消息
1.下载OpenFire服务器,进行安装,参考http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html 2.程序运行客户端:下载客户端代 ...
- 微信技术分享:微信的海量IM聊天消息序列号生成实践(算法原理篇)
1.点评 对于IM系统来说,如何做到IM聊天消息离线差异拉取(差异拉取是为了节省流量).消息多端同步.消息顺序保证等,是典型的IM技术难点. 就像即时通讯网整理的以下IM开发干货系列一样: <I ...
- 【转】编写微信聊天机器人4《聊天精灵WeChatGenius》:实时获取到微信聊天消息,hook数据库插入操作。
接上篇,使用Xposed来hook微信,找到微信进程:https://blog.csdn.net/weixin_42127613/article/details/81839537 既然已经找到了微信进 ...
- 即时通信系统中实现聊天消息加密,让通信更安全【低调赠送:C#开源即时通讯系统(支持广域网)——GGTalk4.5 最新源码】
在即时通讯系统(IM)中,加密重要的通信消息,是一个常见的需求.尤其在一些政府部门的即时通信软件中(如税务系统),对即时聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新 ...
- iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能
类似微信聊天消息中的电话号码点击保存到通讯录中的功能,ABAddress的实现在iOS9中是不能正常使用的,点击完成后,手机会非常的卡,iOS9之后需要使用Contact新提供的方法来实现该功能.快捷 ...
- Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
在文章开始,请你了解和熟悉openfire方面的相关知识,这样对你理解下面代码以及下面代码的用途有很好的了解.同时,你可能需要安装一个简单的CS聊天工具,来测试你的代码是否成功的在openfire服务 ...
- 融云技术分享:解密融云IM产品的聊天消息ID生成策略
本文来自融云技术团队原创分享,原文发布于“融云全球互联网通信云”公众号,原题<如何实现分布式场景下唯一 ID 生成?>,即时通讯网收录时有部分改动. 1.引言 对于IM应用来说,消息ID( ...
- reactnative实现qq聊天消息气泡拖拽消失效果
前言(可跳过) 我在开发自己的APP时遇到了一个类似于qq聊天消息气泡拖拽消息的需求,因为在网上没有找到相关的组件,所以自己动手实现了一下 需求:对聊天消息气泡拖拽到一定长度松开时该气泡会消失(可自行 ...
随机推荐
- IOS键盘的相关设置(UITextfield)
一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKeyboa ...
- CODESOFT 2015中的条形码对象该如何创建
CODESOFT条码设计软件提供了大量适应行业要求的符号,以及创建二维条形码的选项.用户可以通过条形码对话框选择符号.定义其属性以及输入要编码的消息.下面小编带大家具体学习下如何在CODESOFT ...
- Eclipse插件开发之基础篇(4) OSGi框架
转载出处:http://www.cnblogs.com/liuzhuo. 1. 什么是OSGi框架 OSGi(Open Service Gateway Initiative)框架是运行在JavaVM环 ...
- PMP考试--三点估计法
如果你对项目管理.系统架构有兴趣,请加微信订阅号“softjg”,加入这个PM.架构师的大家庭 把施工时间划分为乐观时间.最可能时间.悲观时间 乐观时间:也就是工作顺利情况下的时间为a 最可能时间:最 ...
- Facebook等使用苹果源生分享
1.Facebook官方的SDK分享 2.ShareSDK,第三方集成的分享方式 3.网页分享方式分享 4.IOS6之后,苹果自己集成了对于F ...
- 循环链表Josephus问题(c,cpp)
问题描述: 设有n个人围坐在一个圆桌周围,现从第s个人开始报数,数到第m个的人出列,然后从出列的下一个人重新开始报数,数到第m个的人又出列,.......,如此反复直到所有的人出列为止. Joseph ...
- yii中rights安装
具体安装程序可参考:http://www.focalhot.com/blog/5.html 我在安装过程中一直存在问题,提示表auth_assignment不存在 由于我是将项目中已有的代码复制过来, ...
- 操作笔记:linux下查看端口被占用
[root@iZ945sgm0ugZ /]# lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 1192 jet ...
- oracle学习系列之四 (视图)
视图视图是数据库中特有的对象.视图用于存储查询,但不会存储数据(物化视图除外).这是视图和数据表的重要区别.可以利用视图进行查询,插入,更新和删除数据.Oracle有如下四种视图(关系视图,内嵌视图, ...
- jquery学习记录
1.选择器实例 语法 描述 $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class=&q ...