android qq功能实现原理,Android QQ、微信聊天消息界面设计原理与实现
Android QQ、微信聊天消息界面设计原理与实现
原理:Android平台上,典型的以腾讯的QQ、微信这些聊天消息界面通常可以采用ListView设计与实现,需要使用ListView 适配器Adapter的getItemViewType()和getViewTypeCount()。
在ListView的适配器中,每一次getView时候,首先要判断view的类型getItemViewType(),然后根据不同的类型加载不同的布局view。
至于底部发送消息的窗口,每次发送完消息,需要将ListView滚动到底部,以免输入键盘遮挡住数据而致使用户看不到刚刚发送的消息。
现在给出一个Android平台上简单的设计与实现方案。效果如图所示:
测试的主Activity MainActivity.java:
package zhangphil.chat;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private final int VIEW_TYPE = 0xb01;
private final int VIEW_TYPE_LEFT = -10;
private final int VIEW_TYPE_RIGHT = -11;
private final int MESSAGE = 0xb02;
private ArrayList> items = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(android.R.id.list);
items = new ArrayList>();
for (int i = 0; i < 8; i++) {
if (i % 2 == 0) {
HashMap map = new HashMap();
map.put(VIEW_TYPE, VIEW_TYPE_LEFT);
map.put(MESSAGE, "对方说的消息" + i);
items.add(map);
} else {
HashMap map = new HashMap();
map.put(VIEW_TYPE, VIEW_TYPE_RIGHT);
map.put(MESSAGE, "我说的消息" + i);
items.add(map);
}
}
final MyAdapter adapter = new MyAdapter(this, -1);
listView.setAdapter(adapter);
final EditText msgEditText = (EditText) findViewById(R.id.msgEditText);
Button button = (Button) findViewById(R.id.msgSend);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = msgEditText.getText() + "";
HashMap map = new HashMap();
map.put(VIEW_TYPE, VIEW_TYPE_RIGHT);
map.put(MESSAGE, msg);
items.add(map);
adapter.notifyDataSetChanged();
// 发送后清空输入框内容
msgEditText.setText(null);
// 输入框发送消息后将ListView滚动到最底部
listView.setSelection(ListView.FOCUS_DOWN);
}
});
}
private class MyAdapter extends ArrayAdapter {
private LayoutInflater layoutInflater;
public MyAdapter(Context context, int resource) {
super(context, resource);
layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
int type = getItemViewType(pos);
String msg = getItem(pos);
switch (type) {
case VIEW_TYPE_LEFT:
convertView = layoutInflater.inflate(R.layout.left, null);
TextView textLeft = (TextView) convertView.findViewById(R.id.textView);
textLeft.setText(msg);
break;
case VIEW_TYPE_RIGHT:
convertView = layoutInflater.inflate(R.layout.right, null);
TextView textRight = (TextView) convertView.findViewById(R.id.textView);
textRight.setText(msg);
break;
}
return convertView;
}
@Override
public String getItem(int pos) {
String s = items.get(pos).get(MESSAGE) + "";
return s;
}
@Override
public int getCount() {
return items.size();
}
@Override
public int getItemViewType(int pos) {
int type = (Integer) items.get(pos).get(VIEW_TYPE);
return type;
}
@Override
public int getViewTypeCount() {
return 2;
}
}
}
MainActivity.java需要的布局文件activity_main.xml:
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@android: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" />
android:id="@+id/commentLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#e0e0e0"
android:orientation="horizontal" >
android:id="@+id/msgEditText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="8"
android:hint="发送消息" />
android:id="@+id/msgSend"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="发送" />
ListView适配器Adapter在每一次getView时候,首先判断view的type,然后根据不同的view type加载不同的布局view。
left.xml表示是对方说的消息在ListView界面的左边:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
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" />
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" />
right.xml表示是自己说的消息,在消息聊天界面的右边:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
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" />
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" />
另外,QQ、微信这些聊天消息界面的消息背景是一个气泡,这个气泡其实是一个.9.png图片,将这个气泡的.9.png作为TextView的背景衬图衬上去即可,QQ、微信的聊天气泡.9.png不是本文要着重探讨的内容,在次不再展开叙述。
附参考文章:
《Android ListView Adapter的getItemViewType和getViewTypeCount多种布局》文章链接地址:http://blog.csdn.net/zhangphil/article/details/46984367
android qq功能实现原理,Android QQ、微信聊天消息界面设计原理与实现相关推荐
- Android QQ、微信聊天消息界面设计原理与实现
原理:Android平台上,典型的以腾讯的QQ.微信这些聊天消息界面通常可以采用ListView设计与实现,需要使用ListView 适配器Adapter的getItemViewType()和g ...
- android xml msgid,Android挂逼修练之行–微信聊天消息修改器插件开发详解
一.插件开发背景 昨天无意中在刷抖音的时候看到有个视频可以修改转账信息,我就觉得好奇所以就自己实现了一下,关于微信消息修改其实是个假象就是修改本地自己看到的消息,而不是真正意义上的把聊天信息修改了,但 ...
- Android Studio 开发–微信APP门户界面设计
Android Studio 开发–微信APP门户界面设计 本次Github代码仓库 --crcr1013/MyWechat 文章目录 Android Studio 开发--微信APP门户界面设计 前 ...
- android高仿微信聊天消息列表自由复制文字,双击查看文本内容
SelectTextHelper-高仿微信聊天消息列表自由复制文字,双击查看文本内容 掘金地址 github地址 SelectTextHelper打造一个全网最逼近微信聊天消息自由复制,双击查看文本内 ...
- 微信小程序界面设计入门课程-样式wxss中使用css课程-字体-font-style字体风格
font-style字体风格 微信小程序交流群:111733917 | 微信小程序从0基础到就业的课程:https://edu.csdn.net/topic/huangjuhua 基础用法 font- ...
- 微信小程序界面设计入门课程-样式wxss中使用css课程-文本-direction 文本方向
样式wxss中使用css课程-文本-direction 文本方向 微信小程序交流群:111733917 | 微信小程序从0基础到就业的课程:https://edu.csdn.net/topic/hua ...
- xposed开发8- hook微信聊天消息数据库插入操作
xposed开发8- hook微信聊天消息数据库插入操作 // hook微信聊天消息数据库插入操作 hookClass = "com.tencent.wcdb.database.SQLite ...
- 微信小程序界面设计小程序中的WXSS(css)选择器课程-伪类-:focus 伪类
小程序中的WXSS(css)选择器课程-伪类-:focus 伪类 微信小程序交流群:111733917 | 微信小程序从0基础到就业的课程:https://edu.csdn.net/topic/hua ...
- 微信小程序界面设计小程序中的WXSS(css)选择器课程-伪类-:checked 伪类(不支持)
小程序中的WXSS(css)选择器课程-伪类-:checked 伪类(不支持) 微信小程序交流群:111733917 | 微信小程序从0基础到就业的课程:https://edu.csdn.net/to ...
最新文章
- 位置指纹法的实现(KNN)
- 【CCF】201909-2小明分苹果
- CTS(23)---Camera Media CTS GTS VTS 记录
- MQTT(1)---物联网核心之MQTT(一)
- 455 periodic strings
- GDAL升级到3.0之后遇到的坑
- The Top 8 Security and Risk Trends We’re Watching
- mysql的动态建表_mysql数据库动态创建表的实例分享
- python基本数据类型——set
- 第一个IOS应用终于审核通过了,好开心!
- 全网最全MATLAB学习归纳总结(建模学习必备)
- Python3爬虫中Selenium的用法详解
- QT正则表达式介绍与使用
- js实现23种设计模式
- 一个无经验的大学毕业生,可以转行做程序员吗?我的真实案例
- 荣耀折叠旗舰Magic Vs系列;卡西欧搭配可拆卸表圈的G-SHOCK新品;联发科天玑8200 5G移动芯片 | 科技新品...
- 做个乐意为别人撑伞的人
- linux代码行数统计工具,统计代码行数工具 用golang实现
- 中国当代团员,同先辈相比多了什么
- Smoothie源码编译