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聊天消息气泡拖拽消息的需求,因为在网上没有找到相关的组件,所以自己动手实现了一下 需求:对聊天消息气泡拖拽到一定长度松开时该气泡会消失(可自行 ...
随机推荐
- 基础字符串处理_C++
C++中,有 char [ ] 和 string 两种方式处理字符串 char 数组是最原始的,string 是带迭代器的 正是这种 string 带了迭代器,它会使我们处理字符串很方便,但也十分慢 ...
- Java Web架构知识整理——记一次阿里面试经历
惭愧,从一次电面说起.我个人在某国企做一名软件设计师,国企大家都懂的,待遇一般而且没啥意思,做的方向基本都是操作系统.驱动和工具软件的开发,语言基本都是C/C++.最近也想跳槽,刚好有幸得到了一次阿里 ...
- Android开发-API指南-<category>
<category> 英文原文:http://developer.android.com/guide/topics/manifest/category-element.html 采集(更新 ...
- COM学习(三)——数据类型
上回书介绍了GUID.CLSID.IID和接口的概念.本回的重点是介绍 COM 中的数据类型.咋还不介绍组件程序的设计步骤呀?咳......别着急,别着急!孔子曰:"饭要一口一口地吃&quo ...
- c# 图片路径转byte[] 插到数据库BLOB 图片长宽自定义
//根据图片路径读取图片并且转byte[] 类型 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] byData = ...
- 学习练习 java 集合
将1—100之间的所有正整数存放在一个List集合中,并将集合中索引位置是10的对象从集合中移除 package com.hanqi; import java.util.*; public class ...
- 如何创建下拉列表为一个树列表?(此文为dev控件中,服务器控件暂不知,但想方法应该都差不多吧)
//前端控件代码:<dx:ASPxDropDownEdit ID="drop_treelist" runat="server" ClientInstanc ...
- ASP.NET知识集
ASP.NET知识集 编辑删除转载2015-06-23 16:31:55 标签:it //删除指定行数据时,弹出询问对话框 ((LinkButton)(e.Row.Cell[7].Controls[0 ...
- Code Sign error: No unexpired provisioning profiles found that contain any of the keychain's signing certificates
最近离职了,刚好在离职之际有人叫我帮做个项目,简直了,没有mac电脑,没有真ji设备,简直了.接项目那哥们,暂且叫做J,大哥说我给你想办法,then,给借了个mac pro.刚拿到电脑真是喜出望外啊, ...
- Windows应用替代方案接龙
使开源软件的优势: 开源安全产品的开发.测试和发布过程完全是透明的,同时提供产品的源代码及部分的文档.通过阅读源代码,大家可以清楚地了解开源安全技术的工作原理和实现方法,在选择开源安全技术时更有把握, ...