聊天界面
效果图
主要的界面布局
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:background="#f0f0e0" > 5 6 <RelativeLayout 7 android:id="@+id/rl_layout" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:background="#32bbbb" 11 android:orientation="vertical" > 12 13 <RelativeLayout 14 android:id="@+id/rl_bottom" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:layout_alignParentBottom="true" 18 android:background="@drawable/layout_bg1" > 19 20 <Button 21 android:id="@+id/btn_send" 22 android:layout_width="60dp" 23 android:layout_height="50dp" 24 android:layout_alignParentRight="true" 25 android:layout_centerVertical="true" 26 android:layout_marginRight="10dp" 27 android:text="发送" /> 28 29 <EditText 30 android:id="@+id/et_sendmessage" 31 android:layout_width="fill_parent" 32 android:layout_height="50dp" 33 android:layout_centerVertical="true" 34 android:layout_marginLeft="10dp" 35 android:layout_marginRight="10dp" 36 android:layout_toLeftOf="@id/btn_send" 37 android:background="@drawable/edittext1" 38 android:singleLine="true" 39 android:textSize="18sp" /> 40 </RelativeLayout> 41 42 <ListView 43 android:id="@+id/listview" 44 android:layout_width="fill_parent" 45 android:layout_height="fill_parent" 46 android:layout_above="@id/rl_bottom" 47 android:layout_marginLeft="10.0dip" 48 android:layout_marginRight="10.0dip" 49 android:layout_marginTop="10.0dip" 50 android:cacheColorHint="#00000000" 51 android:divider="@null" 52 android:dividerHeight="5dp" 53 android:scrollbars="none" /> 54 </RelativeLayout> 55 </RelativeLayout>
item布局(这里只是一边的)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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/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/chatfrom_bg" /> <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>
java主界面
1 package com.example.administrator.yunphone.View; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.support.annotation.Nullable; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.ListView; 12 13 import com.example.administrator.yunphone.R; 14 import com.example.administrator.yunphone.UI.ChatMsgEntity; 15 import com.example.administrator.yunphone.UI.ChatMsgViewAdapter; 16 17 import java.text.SimpleDateFormat; 18 import java.util.ArrayList; 19 import java.util.Date; 20 import java.util.List; 21 22 /** 23 * Created by Administrator on 2016/7/13. 24 */ 25 public class MsgFragment extends Fragment implements View.OnClickListener { 26 private View mView; 27 28 private Button mBtnSend;// 发送btn 29 private EditText mEditTextContent; 30 private ListView mListView; 31 private ChatMsgViewAdapter mAdapter;// 消息视图的Adapter 32 private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();// 消息对象数组 33 34 @Nullable 35 @Override 36 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 37 mView = inflater.inflate(R.layout.fragment_msg_layout, null); 38 39 initView(); 40 initData(); 41 return mView; 42 } 43 44 /** 45 * 初始化view 46 */ 47 public void initView() { 48 mListView = (ListView) mView.findViewById(R.id.listview); 49 mBtnSend = (Button) mView.findViewById(R.id.btn_send); 50 mBtnSend.setOnClickListener(this); 51 mEditTextContent = (EditText) mView.findViewById(R.id.et_sendmessage); 52 } 53 private String[] msgArray = new String[] { "有大吗", "有!你呢?", "我也有", "那上吧", 54 "打啊!你放大啊!", "你TM咋不放大呢?留大抢人头啊?CAO!你个菜B", "2B不解释", "尼滚...", 55 "今晚去网吧包夜吧?", "有毛片吗?", "种子一大堆啊~还怕没片?", "OK,搞起!!" }; 56 57 private String[] dataArray = new String[] { "2012-09-22 18:00:02", 58 "2012-09-22 18:10:22", "2012-09-22 18:11:24", 59 "2012-09-22 18:20:23", "2012-09-22 18:30:31", 60 "2012-09-22 18:35:37", "2012-09-22 18:40:13", 61 "2012-09-22 18:50:26", "2012-09-22 18:52:57", 62 "2012-09-22 18:55:11", "2012-09-22 18:56:45", 63 "2012-09-22 18:57:33", }; 64 private final static int COUNT = 12;// 初始化数组总数 65 66 /** 67 * 模拟加载消息历史,实际开发可以从数据库中读出 68 */ 69 public void initData() { 70 for (int i = 0; i < COUNT; i++) { 71 ChatMsgEntity entity = new ChatMsgEntity(); 72 entity.setDate(dataArray[i]); 73 if (i % 2 == 0) { 74 entity.setName("肖B"); 75 entity.setMsgType(true);// 收到的消息 76 } else { 77 entity.setName("必败"); 78 entity.setMsgType(false);// 自己发送的消息 79 } 80 entity.setMessage(msgArray[i]); 81 mDataArrays.add(entity); 82 } 83 84 mAdapter = new ChatMsgViewAdapter(getActivity(), mDataArrays); 85 mListView.setAdapter(mAdapter); 86 } 87 88 @Override 89 public void onClick(View v) { 90 switch (v.getId()) { 91 case R.id.btn_send:// 发送按钮点击事件 92 send(); 93 break; 94 } 95 } 96 /** 97 * 发送消息 98 */ 99 private void send() { 100 String contString = mEditTextContent.getText().toString(); 101 if (contString.length() > 0) { 102 ChatMsgEntity entity = new ChatMsgEntity(); 103 entity.setName("必败"); 104 entity.setDate(getDate()); 105 entity.setMessage(contString); 106 entity.setMsgType(false); 107 108 mDataArrays.add(entity); 109 mAdapter.notifyDataSetChanged();// 通知ListView,数据已发生改变 110 mEditTextContent.setText("");// 清空编辑框数据 111 mListView.setSelection(mListView.getCount() - 1);// 发送一条消息时,ListView显示选择最后一项 112 } 113 } 114 115 /** 116 * 发送消息时,获取当前事件 117 * 118 * @return 当前时间 119 */ 120 private String getDate() { 121 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 122 return format.format(new Date()); 123 } 124 }
自定义List适配器
package com.example.administrator.yunphone.UI; 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; import com.example.administrator.yunphone.R; /** * 消息ListView的Adapter * * @author way */ public class ChatMsgViewAdapter extends BaseAdapter { public static interface IMsgViewType { int IMVT_COM_MSG = 0;// 收到对方的消息 int IMVT_TO_MSG = 1;// 自己发送出去的消息 } private static final int ITEMCOUNT = 2;// 消息类型的总数 private List<ChatMsgEntity> coll;// 消息对象数组 private LayoutInflater mInflater; public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> coll) { this.coll = coll; mInflater = LayoutInflater.from(context); } public int getCount() { return coll.size(); } public Object getItem(int position) { return coll.get(position); } public long getItemId(int position) { return position; } /** * 得到Item的类型,是对方发过来的消息,还是自己发送出去的 */ public int getItemViewType(int position) { ChatMsgEntity entity = coll.get(position); if (entity.getMsgType()) {//收到的消息 return IMsgViewType.IMVT_COM_MSG; } else {//自己发送的消息 return IMsgViewType.IMVT_TO_MSG; } } /** * Item类型的总数 */ public int getViewTypeCount() { return ITEMCOUNT; } 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.chatting_item_msg_text_left, null); } else { convertView = mInflater.inflate( R.layout.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类
1 package com.example.administrator.yunphone.UI; 2 3 /** 4 * 一个消息的JavaBean 5 * 6 */ 7 public class ChatMsgEntity { 8 private String name;//消息来自 9 private String date;//消息日期 10 private String message;//消息内容 11 private boolean isComMeg = true;// 是否为收到的消息 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public String getDate() { 22 return date; 23 } 24 25 public void setDate(String date) { 26 this.date = date; 27 } 28 29 public String getMessage() { 30 return message; 31 } 32 33 public void setMessage(String message) { 34 this.message = message; 35 } 36 37 public boolean getMsgType() { 38 return isComMeg; 39 } 40 41 public void setMsgType(boolean isComMsg) { 42 isComMeg = isComMsg; 43 } 44 45 public ChatMsgEntity() { 46 } 47 48 public ChatMsgEntity(String name, String date, String text, boolean isComMsg) { 49 super(); 50 this.name = name; 51 this.date = date; 52 this.message = text; 53 this.isComMeg = isComMsg; 54 } 55 56 }