WeixinChatDemo
main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:background="#f0f0e0" > 6 7 <RelativeLayout 8 android:id="@+id/rl_layout" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:background="@drawable/mmtitle_bg" 12 android:orientation="vertical" > 13 14 <Button 15 android:id="@+id/btn_back" 16 android:layout_width="70dp" 17 android:layout_height="wrap_content" 18 android:layout_centerVertical="true" 19 android:layout_marginLeft="10dp" 20 android:background="@drawable/selector_btn_back" 21 android:paddingLeft="10dp" 22 android:text="返回" 23 android:textColor="#ffffff" 24 android:textSize="18sp" /> 25 26 <TextView 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_centerInParent="true" 30 android:text="way" 31 android:textColor="#ffffff" 32 android:textSize="20.0sp" /> 33 </RelativeLayout> 34 35 <RelativeLayout 36 android:id="@+id/rl_bottom" 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:layout_alignParentBottom="true" 40 android:background="@drawable/layout_bg1" > 41 42 <Button 43 android:id="@+id/btn_send" 44 android:layout_width="60dp" 45 android:layout_height="40dp" 46 android:layout_alignParentRight="true" 47 android:layout_centerVertical="true" 48 android:layout_marginRight="10dp" 49 android:text="发送" /> 50 51 <EditText 52 android:id="@+id/et_sendmessage" 53 android:layout_width="fill_parent" 54 android:layout_height="40dp" 55 android:layout_centerVertical="true" 56 android:layout_marginLeft="10dp" 57 android:layout_marginRight="10dp" 58 android:layout_toLeftOf="@id/btn_send" 59 android:background="@drawable/edittext1" 60 android:singleLine="true" 61 android:textSize="18sp" /> 62 </RelativeLayout> 63 64 <ListView 65 android:id="@+id/listview" 66 android:layout_width="fill_parent" 67 android:layout_height="fill_parent" 68 android:layout_above="@id/rl_bottom" 69 android:layout_below="@id/rl_layout" 70 android:layout_marginLeft="10.0dip" 71 android:layout_marginRight="10.0dip" 72 android:layout_marginTop="10.0dip" 73 android:cacheColorHint="#00000000" 74 android:divider="@null" 75 android:dividerHeight="5dp" 76 android:scrollbars="none" /> 77 78 </RelativeLayout>
WeixinChatDemoActivity
1 package com.way.demo; 2 3 import java.text.SimpleDateFormat; 4 import java.util.ArrayList; 5 import java.util.Date; 6 import java.util.List; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.EditText; 14 import android.widget.ListView; 15 16 /** 17 * @author way 18 */ 19 public class WeixinChatDemoActivity extends Activity implements OnClickListener { 20 21 private Button mBtnSend;// 发送btn 22 private Button mBtnBack;// 返回btn 23 private EditText mEditTextContent; 24 private ListView mListView; 25 private ChatMsgViewAdapter mAdapter;// 消息视图的Adapter 26 private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();// 消息对象数组 27 28 public void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.main); 31 32 initView();// 初始化view 33 34 initData();// 初始化数据 35 mListView.setSelection(mAdapter.getCount() - 1); 36 } 37 38 /** 39 * 初始化view 40 */ 41 public void initView() { 42 mListView = (ListView) findViewById(R.id.listview); 43 mBtnSend = (Button) findViewById(R.id.btn_send); 44 mBtnSend.setOnClickListener(this); 45 mBtnBack = (Button) findViewById(R.id.btn_back); 46 mBtnBack.setOnClickListener(this); 47 mEditTextContent = (EditText) findViewById(R.id.et_sendmessage); 48 } 49 50 private String[] msgArray = new String[] { "有大吗", "有!你呢?", "我也有", "那上吧", 51 "打啊!你放大啊!", "你TM咋不放大呢?留大抢人头啊?CAO!你个菜B", "2B不解释", "尼滚...", 52 "今晚去网吧包夜吧?", "有毛片吗?", "种子一大堆啊~还怕没片?", "OK,搞起!!" }; 53 54 private String[] dataArray = new String[] { "2012-09-22 18:00:02", 55 "2012-09-22 18:10:22", "2012-09-22 18:11:24", 56 "2012-09-22 18:20:23", "2012-09-22 18:30:31", 57 "2012-09-22 18:35:37", "2012-09-22 18:40:13", 58 "2012-09-22 18:50:26", "2012-09-22 18:52:57", 59 "2012-09-22 18:55:11", "2012-09-22 18:56:45", 60 "2012-09-22 18:57:33", }; 61 private final static int COUNT = 12;// 初始化数组总数 62 63 /** 64 * 模拟加载消息历史,实际开发可以从数据库中读出 65 */ 66 public void initData() { 67 for (int i = 0; i < COUNT; i++) { 68 ChatMsgEntity entity = new ChatMsgEntity(); 69 entity.setDate(dataArray[i]); 70 if (i % 2 == 0) { 71 entity.setName("肖B"); 72 entity.setMsgType(true);// 收到的消息 73 } else { 74 entity.setName("必败"); 75 entity.setMsgType(false);// 自己发送的消息 76 } 77 entity.setMessage(msgArray[i]); 78 mDataArrays.add(entity); 79 } 80 81 mAdapter = new ChatMsgViewAdapter(this, mDataArrays); 82 mListView.setAdapter(mAdapter); 83 } 84 85 @Override 86 public void onClick(View v) { 87 switch (v.getId()) { 88 case R.id.btn_send:// 发送按钮点击事件 89 send(); 90 break; 91 case R.id.btn_back:// 返回按钮点击事件 92 finish();// 结束,实际开发中,可以返回主界面 93 break; 94 } 95 } 96 97 /** 98 * 发送消息 99 */ 100 private void send() { 101 String contString = mEditTextContent.getText().toString(); 102 if (contString.length() > 0) { 103 ChatMsgEntity entity = new ChatMsgEntity(); 104 entity.setName("必败"); 105 entity.setDate(getDate()); 106 entity.setMessage(contString); 107 entity.setMsgType(false); 108 109 mDataArrays.add(entity); 110 mAdapter.notifyDataSetChanged();// 通知ListView,数据已发生改变 111 112 mEditTextContent.setText("");// 清空编辑框数据 113 114 mListView.setSelection(mListView.getCount() - 1);// 发送一条消息时,ListView显示选择最后一项 115 } 116 } 117 118 /** 119 * 发送消息时,获取当前事件 120 * 121 * @return 当前时间 122 */ 123 private String getDate() { 124 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 125 return format.format(new Date()); 126 } 127 }
ChatMsgViewAdapter
1 package com.way.demo; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.TextView; 11 12 13 /** 14 * 消息ListView的Adapter 15 * 16 * @author way 17 */ 18 public class ChatMsgViewAdapter extends BaseAdapter { 19 20 public static interface IMsgViewType { 21 int IMVT_COM_MSG = 0;// 收到对方的消息 22 int IMVT_TO_MSG = 1;// 自己发送出去的消息 23 } 24 25 private static final int ITEMCOUNT = 2;// 消息类型的总数 26 private List<ChatMsgEntity> coll;// 消息对象数组 27 private LayoutInflater mInflater; 28 29 public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> coll) { 30 this.coll = coll; 31 mInflater = LayoutInflater.from(context); 32 } 33 34 public int getCount() { 35 return coll.size(); 36 } 37 38 public Object getItem(int position) { 39 return coll.get(position); 40 } 41 42 public long getItemId(int position) { 43 return position; 44 } 45 46 /** 47 * 得到Item的类型,是对方发过来的消息,还是自己发送出去的 48 */ 49 public int getItemViewType(int position) { 50 ChatMsgEntity entity = coll.get(position); 51 52 if (entity.getMsgType()) {//收到的消息 53 return IMsgViewType.IMVT_COM_MSG; 54 } else {//自己发送的消息 55 return IMsgViewType.IMVT_TO_MSG; 56 } 57 } 58 59 /** 60 * Item类型的总数 61 */ 62 public int getViewTypeCount() { 63 return ITEMCOUNT; 64 } 65 66 public View getView(int position, View convertView, ViewGroup parent) { 67 68 ChatMsgEntity entity = coll.get(position); 69 boolean isComMsg = entity.getMsgType(); 70 71 ViewHolder viewHolder = null; 72 if (convertView == null) { 73 if (isComMsg) { 74 convertView = mInflater.inflate( 75 R.layout.chatting_item_msg_text_left, null); 76 } else { 77 convertView = mInflater.inflate( 78 R.layout.chatting_item_msg_text_right, null); 79 } 80 81 viewHolder = new ViewHolder(); 82 viewHolder.tvSendTime = (TextView) convertView 83 .findViewById(R.id.tv_sendtime); 84 viewHolder.tvUserName = (TextView) convertView 85 .findViewById(R.id.tv_username); 86 viewHolder.tvContent = (TextView) convertView 87 .findViewById(R.id.tv_chatcontent); 88 viewHolder.isComMsg = isComMsg; 89 90 convertView.setTag(viewHolder); 91 } else { 92 viewHolder = (ViewHolder) convertView.getTag(); 93 } 94 viewHolder.tvSendTime.setText(entity.getDate()); 95 viewHolder.tvUserName.setText(entity.getName()); 96 viewHolder.tvContent.setText(entity.getMessage()); 97 return convertView; 98 } 99 100 static class ViewHolder { 101 public TextView tvSendTime; 102 public TextView tvUserName; 103 public TextView tvContent; 104 public boolean isComMsg = true; 105 } 106 107 }
chatting_item_msg_text_left.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:orientation="vertical" 6 android:padding="6dp" > 7 8 <LinearLayout 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:gravity="center_horizontal" 12 android:orientation="vertical" > 13 14 <TextView 15 android:id="@+id/tv_sendtime" 16 style="@style/chat_text_date_style" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" /> 19 </LinearLayout> 20 21 <RelativeLayout 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:layout_marginTop="5dp" > 25 26 <ImageView 27 android:id="@+id/iv_userhead" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_alignParentLeft="true" 31 android:layout_alignParentTop="true" 32 android:background="@drawable/mini_avatar_shadow" 33 android:focusable="false" /> 34 35 <TextView 36 android:id="@+id/tv_chatcontent" 37 style="@style/chat_content_date_style" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:layout_marginLeft="10dp" 41 android:layout_toRightOf="@id/iv_userhead" 42 android:background="@drawable/chatfrom_bg" /> 43 44 <TextView 45 android:id="@+id/tv_username" 46 style="@style/chat_text_name_style" 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:layout_alignParentLeft="true" 50 android:layout_below="@id/iv_userhead" 51 android:layout_toLeftOf="@id/tv_chatcontent" /> 52 </RelativeLayout> 53 54 </LinearLayout>
chatting_item_msg_text_right.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:orientation="vertical" 6 android:padding="6dp" > 7 8 <LinearLayout 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:gravity="center_horizontal" 12 android:orientation="vertical" > 13 14 <TextView 15 android:id="@+id/tv_sendtime" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:background="#bfbfbf" 19 android:padding="2dp" 20 android:textColor="#ffffff" 21 android:textSize="12sp" /> 22 </LinearLayout> 23 24 <RelativeLayout 25 android:layout_width="fill_parent" 26 android:layout_height="wrap_content" 27 android:layout_marginTop="5dp" > 28 29 <ImageView 30 android:id="@+id/iv_userhead" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_alignParentRight="true" 34 android:layout_alignParentTop="true" 35 android:background="@drawable/mini_avatar_shadow" 36 android:focusable="false" /> 37 38 <TextView 39 android:id="@+id/tv_chatcontent" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:layout_marginRight="10dp" 43 android:layout_toLeftOf="@id/iv_userhead" 44 android:background="@drawable/chatto_bg" 45 android:clickable="true" 46 android:focusable="true" 47 android:gravity="left|center" 48 android:lineSpacingExtra="2dp" 49 android:minHeight="50dp" 50 android:textColor="#ff000000" 51 android:textSize="15sp" /> 52 53 <TextView 54 android:id="@+id/tv_username" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:layout_alignParentRight="true" 58 android:layout_below="@id/iv_userhead" 59 android:layout_toRightOf="@id/tv_chatcontent" 60 android:gravity="center" 61 android:textColor="#818181" 62 android:textSize="15sp" /> 63 </RelativeLayout> 64 65 </LinearLayout>