相似微信的ChattingUi
先看主页面布局
activity_imitate_weixin_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0e0" > <RelativeLayout
android:id="@+id/rl_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/weixin_layout_bg1" > <Button
android:id="@+id/btn_send"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="发送" /> <EditText
android:id="@+id/et_sendmessage"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/btn_send"
android:background="@drawable/weixin_edittext1"
android:singleLine="true"
android:textSize="18sp" />
</RelativeLayout> <ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/rl_bottom"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="10.0dip"
android:layout_marginTop="10.0dip"
android:cacheColorHint="#00000000"
android:divider="@null"
android:dividerHeight="5dp"
android:scrollbars="none" /> </RelativeLayout>
再看入口WeixinChatDemoActivity
package com.example.weixindemo; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
/**
* 仿微信主页面
*/
public class WeixinChatDemoActivity extends Activity implements OnClickListener { private Button mBtnSend;// 发送btn
private EditText mEditTextContent;
private ListView mListView;
private ChatMsgViewAdapter mAdapter;// 消息视图的Adapter
private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();// 消息对象数组 private final static int COUNT = 1;// 初始化数组总数 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imitate_weixin_main);
initView();
} public void initView() {
mListView = (ListView) findViewById(R.id.listview);
mBtnSend = (Button) findViewById(R.id.btn_send);
mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);
initData();// 初始化数据 mBtnSend.setOnClickListener(this);
mListView.setSelection(mAdapter.getCount() - 1);
} /**
* 模拟载入消息历史,实际开发能够从数据库中读出
*/
public void initData() {
for (int i = 0; i < COUNT; i++) {
ChatMsgEntity entity = new ChatMsgEntity();
entity.setDate("2012-09-22 18:00:02");
if (i % 2 == 0) {
entity.setName("他人");
entity.setMsgType(true);// 收到的消息
} else {
entity.setName("本人");
entity.setMsgType(false);// 自己发送的消息
}
entity.setMessage("今晚去网吧包夜吧?");
mDataArrays.add(entity);
} mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
mListView.setAdapter(mAdapter);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send:// 发送button点击事件
send();
break;
}
} /**
* 发送消息
*/
private void send() {
String contString = mEditTextContent.getText().toString();
if (contString.length() > 0) {
ChatMsgEntity entity = new ChatMsgEntity();
entity.setName("本人");
entity.setDate(getDate());
entity.setMessage(contString);
entity.setMsgType(false); mDataArrays.add(entity);
mAdapter.notifyDataSetChanged();// 通知ListView,数据已发生改变 mEditTextContent.setText("");// 清空编辑框数据 mListView.setSelection(mListView.getCount() - 1);// 发送一条消息时,ListView显示选择最后一项
}
} /**
* 发送消息时,获取当前事件
*
* @return 当前时间
*/
private String getDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(new Date());
} }
再看适配器
ChatMsgViewAdapter
package com.example.weixindemo;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 消息ListView的Adapter
*/
public class ChatMsgViewAdapter extends BaseAdapter { private List<ChatMsgEntity> coll;// 消息对象数组
private LayoutInflater mInflater; public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> coll) {
this.coll = coll;
mInflater = LayoutInflater.from(context);
} /*****************************************************/
//得到Item的类型。是对方发过来的消息,还是自己发送出去的
public int getItemViewType(int position) {
return coll.get(position).getMsgType()?1:0;
}
//Item类型的总数
public int getViewTypeCount() {
return 2;
}
/******************************************************/
public int getCount() {
return coll.size();
} public Object getItem(int position) {
return coll.get(position);
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) { ChatMsgEntity entity = coll.get(position);
boolean isComMsg = entity.getMsgType(); ViewHolder viewHolder = null;
if (convertView == null) {
if (isComMsg) {
convertView = mInflater.inflate(
R.layout.activity_imitate_weixin_chatting_item_msg_text_left, null);
} else {
convertView = mInflater.inflate(
R.layout.activity_imitate_weixin_chatting_item_msg_text_right, null);
} viewHolder = new ViewHolder();
viewHolder.tvSendTime = (TextView) convertView
.findViewById(R.id.tv_sendtime);
viewHolder.tvUserName = (TextView) convertView
.findViewById(R.id.tv_username);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tv_chatcontent);
viewHolder.isComMsg = isComMsg; convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvSendTime.setText(entity.getDate());
viewHolder.tvUserName.setText(entity.getName());
viewHolder.tvContent.setText(entity.getMessage());
return convertView;
} static class ViewHolder {
public TextView tvSendTime;
public TextView tvUserName;
public TextView tvContent;
public boolean isComMsg = true;
} }
另外还辅助的bean类
ChatMsgEntity
package com.example.weixindemo; /**
* 一个消息的JavaBean
*/
public class ChatMsgEntity {
private String name;//消息来自
private String date;//消息日期
private String message;//消息内容
private boolean isComMeg = true;// 是否为收到的消息 public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public boolean getMsgType() {
return isComMeg;
} public void setMsgType(boolean isComMsg) {
isComMeg = isComMsg;
} public ChatMsgEntity() {
} public ChatMsgEntity(String name, String date, String text, boolean isComMsg) {
super();
this.name = name;
this.date = date;
this.message = text;
this.isComMeg = isComMsg;
} }
另外还有聊天界面本人、他人的布局文件
activity_imitate_weixin_chatting_item_msg_text_left.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" > <TextView
android:id="@+id/tv_sendtime"
style="@style/chat_text_date_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" > <ImageView
android:id="@+id/iv_userhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/weixin_mini_avatar_shadow"
android:focusable="false" /> <TextView
android:id="@+id/tv_chatcontent"
style="@style/chat_content_date_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_userhead"
android:background="@drawable/weixin_chatfrom_bg_normal" /> <TextView
android:id="@+id/tv_username"
style="@style/chat_text_name_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/iv_userhead"
android:layout_toLeftOf="@id/tv_chatcontent" />
</RelativeLayout> </LinearLayout>
activity_imitate_weixin_chatting_item_msg_text_right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" > <TextView
android:id="@+id/tv_sendtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#bfbfbf"
android:padding="2dp"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" > <ImageView
android:id="@+id/iv_userhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/weixin_mini_avatar_shadow"
android:focusable="false" /> <TextView
android:id="@+id/tv_chatcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/iv_userhead"
android:background="@drawable/weixin_chatto_bg_normal"
android:clickable="true"
android:focusable="true"
android:gravity="left|center"
android:lineSpacingExtra="2dp"
android:minHeight="50dp"
android:textColor="#ff000000"
android:textSize="15sp" /> <TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/iv_userhead"
android:layout_toRightOf="@id/tv_chatcontent"
android:gravity="center"
android:textColor="#818181"
android:textSize="15sp" />
</RelativeLayout> </LinearLayout>
相似微信的ChattingUi的更多相关文章
- 微信企业号 获取AccessToken
目录 1. AccessToken介绍 2. 示例代码 1. AccessToken介绍 1.1 什么是AccessToken AccessToken即访问凭证,业务服务器每次主动调用企业号接口时需要 ...
- 微信小程序开发心得
微信小程序也已出来有一段时间了,最近写了几款微信小程序项目,今天来说说感受. 首先开发一款微信小程序,最主要的就是针对于公司来运营的,因为,在申请appid(微信小程序ID号)时候,需要填写相关的公司 ...
- 微信公众号开发之VS远程调试
目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 前言 微信公众平台消息接口的工作原理大概可以这样理解:从用户端到公众号端一个流 ...
- 微信应用号(小程序)开发IDE配置(第一篇)
2016年9月22日凌晨,微信宣布“小程序”问世,当然只是开始内测了,微信公众平台对200个服务号发送了小程序内测邀请.那么什么是“小程序”呢,来看微信之父怎么说 看完之后,相信大家大概都有些明白了吧 ...
- SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...
- SQLSERVER走起微信公众帐号全新改版 全新首页
SQLSERVER走起微信公众帐号全新改版 全新首页 今天,SQLSERVER走起微信公众帐号增加了首页功能 虽然还是订阅号,不过已经对版面做了比较大的修改,希望各位亲用得放心.用得安心O(∩_∩)O ...
- 微信小程序体验(2):驴妈妈景区门票即买即游
驴妈妈因为出色的运营能力,被腾讯选为首批小程序内测单位.驴妈妈的技术开发团队在很短的时间内完成了开发任务,并积极参与到张小龙团队的内测问题反馈.驴妈妈认为,移动互联网时代,微信是巨大的流量入口,也是旅 ...
- WPF 微信 MVVM
公司的同事离职了,接下来的日子可能会忙碌,能完善DEMO的时间也会少了,因此,把做的简易DEMO整体先记录一下,等后续不断的完善. 参考两位大神的日志:WEB版微信协议部分功能分析.[完全开源]微信客 ...
- WPF 微信 MVVM 【续】修复部分用户无法获取列表
看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...
随机推荐
- STL iterator和reverse_iterator
先看一段代码: #include <iostream> #include <deque> #include <algorithm> #include <ite ...
- js版根据经纬度计算多边形面积(墨卡托投影)
[摘要:var earthRadiusMeters = 6371000.0; var metersPerDegree = 2.0 * Math.PI * earthRadiusMeters / 360 ...
- IIS服务器支持.apk文件下载
随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站客户端,.apk文件就是安卓(Android)的应用程序后缀名,默认情况下,使用IIS作为Web服务器的无法下载此文件 ...
- 如何使用Linux匿名上网-四大法宝
信息时代给我们的生活带来极大便利和好处的同时也带来了很大的风险.一方面,人们只要点击几下按钮,就能基本上访问已知存在的全部信息和知识;另一方面,要是这种权力落到个别不法分子手里,就会引起重大破坏和灾难 ...
- Windows Xp不用安装软件管理多个远程桌面连接
一直使用系统默认的Mstsc来进行远程连接,但如果要连接N个远程的话就比较麻烦 之前也找过第三方的管理软件如:mRemoteNG 此软件有优点就不说了,但我在使用此软件时有一个很大的问题,就是如果一个 ...
- HTML5 Canvas(画布)实战编程初级篇:基本介绍和基础画布元素
欢迎大家阅读HTML5 Canvas(画布)实战编程初级篇系列,在这个系列中,我们将介绍最简单的HTML5画布编程.包括: 画布元素 绘制直线 绘制曲线 绘制路径 绘制图形 绘制颜色,渐变和图案 绘制 ...
- 关于Csdn水区被占据一事 (2015-08-01)
例如以下图所看到的 水区被占据 ,假设发贴机不仅仅在水区发贴.也在其他版块也发贴,将不堪设想啊各位. 如今非常多站点也经历过被 注冊机,发贴机,乱炸,是非常可恨的事.可是您想想.为什么注冊机.发贴机会 ...
- oracle 存储过程 示例
oracle 存储过程 示例 CreationTime--2018年9月4日09点49分 Author:Marydon 1.情景展示 对VIRTUAL_QRCODELOG表的静态二维码,动态二维码 ...
- java 动态代理(模式) InvocationHandler(为类中方法执行前或后添加内容)
动态代理属于Java反射的一种. 当我们得到一个对象,想动态的为其一些方法每次被调用前后追加一些操作时,我们将会用到java动态代理. 下边上代码: 首先定义一个接口: package com.liu ...
- 转:TCP/IP协议选项——TCP_KEEPALIVE .
[+] KEEPALIVE作用 KEEPALIVE代码示例 KEEPALIVE脚本设置 1.KEEPALIVE作用 KEEPALIVE机制,是TCP协议规定的TCP层(非应用层业务代码实现的)检测 ...