在这一章咱们来分析一下聊天界面中消息的显示,MessageAdapter。
public MessageAdapter(Context context, String username, int chatType) {
this.username = username;
this.context = context;
inflater = LayoutInflater.from(context);
activity = (Activity) context;
this.conversation = EMChatManager.getInstance().getConversation(username);
}
其中构造方法中是对成员变量的赋值。
并且在适配器里面还声明了一个Handler:
Handler handler = new Handler() {
private void refreshList() {
// UI线程不能直接使用conversation.getAllMessages()
// 否则在UI刷新过程中,如果收到新的消息,会导致并发问题
messages = (EMMessage[]) conversation.getAllMessages().toArray(new EMMessage[conversation.getAllMessages().size()]);
for (int i = 0; i < messages.length; i++) {
// getMessage will set message as read status
conversation.getMessage(i);
}
notifyDataSetChanged();
}
@Override
public void handleMessage(android.os.Message message) {
switch (message.what) {
case HANDLER_MESSAGE_REFRESH_LIST:
refreshList();
break;
case HANDLER_MESSAGE_SELECT_LAST:
if (activity instanceof ChatActivity) {
ListView listView = ((ChatActivity) activity).getListView();
if (messages.length > 0) {
listView.setSelection(messages.length - 1);
}
}
break;
case HANDLER_MESSAGE_SEEK_TO:
int position = message.arg1;
if (activity instanceof ChatActivity) {
ListView listView = ((ChatActivity) activity).getListView();
listView.setSelection(position);
}
break;
default:
break;
}
}
};
当传递过来的消息为HANDLER_MESSAGE_REFRESH_LIST时,调用refreshList方法刷新UI。当传递过来的消息为HANDLER_MESSAGE_SELECT_LAST时,将ListVIew滚动到最后一项。当传递过来的消息为HANDLER_MESSAGE_SEEK_TO时,将ListView滚动到指定位置。
/**
* 获取item数
*/
public int getCount() {
return messages == null ? 0 : messages.length;
}
如果没有消息返回0.
/**
* 刷新页面
*/
public void refresh() {
if (handler.hasMessages(HANDLER_MESSAGE_REFRESH_LIST)) {
return;
}
android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST);
handler.sendMessage(msg);
}
如果Handler中没有HANDLER_MESSAGE_REFRESH_LIST消息的时候发送此消息。
/**
* 刷新页面, 选择最后一个
*/
public void refreshSelectLast() {
handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST));
handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_SELECT_LAST));
}
/**
* 刷新页面, 选择Position
*/
public void refreshSeekTo(int position) {
handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST));
android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_SEEK_TO);
msg.arg1 = position;
handler.sendMessage(msg);
}
更改UI前先刷新UI。
public EMMessage getItem(int position) {
if (messages != null && position < messages.length) {
return messages[position];
}
return null;
}
public long getItemId(int position) {
return position;
}
Adapter中需要重载的方法。
private static final int MESSAGE_TYPE_RECV_TXT = 0;
private static final int MESSAGE_TYPE_SENT_TXT = 1;
private static final int MESSAGE_TYPE_SENT_IMAGE = 2;
private static final int MESSAGE_TYPE_SENT_LOCATION = 3;
private static final int MESSAGE_TYPE_RECV_LOCATION = 4;
private static final int MESSAGE_TYPE_RECV_IMAGE = 5;
private static final int MESSAGE_TYPE_SENT_VOICE = 6;
private static final int MESSAGE_TYPE_RECV_VOICE = 7;
private static final int MESSAGE_TYPE_SENT_VIDEO = 8;
private static final int MESSAGE_TYPE_RECV_VIDEO = 9;
private static final int MESSAGE_TYPE_SENT_FILE = 10;
private static final int MESSAGE_TYPE_RECV_FILE = 11;
private static final int MESSAGE_TYPE_SENT_VOICE_CALL = 12;
private static final int MESSAGE_TYPE_RECV_VOICE_CALL = 13;
private static final int MESSAGE_TYPE_SENT_VIDEO_CALL = 14;
private static final int MESSAGE_TYPE_RECV_VIDEO_CALL = 15;
/**
* 获取item类型数
*/
public int getViewTypeCount() {
return 16;
}
获取消息类型的个数。
/**
* 获取item类型
*/
public int getItemViewType(int position) {
EMMessage message = getItem(position);
if (message == null) {
return -1;
}
if (message.getType() == EMMessage.Type.TXT) {
if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false))
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE_CALL : MESSAGE_TYPE_SENT_VOICE_CALL;
else if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false))
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO_CALL : MESSAGE_TYPE_SENT_VIDEO_CALL;
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_TXT : MESSAGE_TYPE_SENT_TXT;
}
if (message.getType() == EMMessage.Type.IMAGE) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_IMAGE : MESSAGE_TYPE_SENT_IMAGE;
}
if (message.getType() == EMMessage.Type.LOCATION) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_LOCATION : MESSAGE_TYPE_SENT_LOCATION;
}
if (message.getType() == EMMessage.Type.VOICE) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE : MESSAGE_TYPE_SENT_VOICE;
}
if (message.getType() == EMMessage.Type.VIDEO) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO : MESSAGE_TYPE_SENT_VIDEO;
}
if (message.getType() == EMMessage.Type.FILE) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_FILE : MESSAGE_TYPE_SENT_FILE;
}
return -1;// invalid
}
通过SDK来判断具体是发送的消息还是接收的消息。
private View createViewByMessage(EMMessage message, int position) {
switch (message.getType()) {
case LOCATION:
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_location, null) : inflater.inflate(R.layout.row_sent_location, null);
case IMAGE:
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_picture, null) : inflater.inflate(R.layout.row_sent_picture, null);
case VOICE:
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_voice, null) : inflater.inflate(R.layout.row_sent_voice, null);
case VIDEO:
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_video, null) : inflater.inflate(R.layout.row_sent_video, null);
case FILE:
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_file, null) : inflater.inflate(R.layout.row_sent_file, null);
default:
// 语音通话
if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false))
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_voice_call, null) : inflater.inflate(R.layout.row_sent_voice_call, null);
// 视频通话
else if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false))
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_video_call, null) : inflater.inflate(R.layout.row_sent_video_call, null);
return message.direct == EMMessage.Direct.RECEIVE ? inflater.inflate(R.layout.row_received_message, null) : inflater.inflate(R.layout.row_sent_message, null);
}
}
根据不同的消息类型来创建不同的布局,因为消息内容可能是文本、或者是视频等,而且可能是发过来的,或者是发送出去的。所以要有不同的布局文件。
接下来就是最主要的方法了getView:
public View getView(final int position, View convertView, ViewGroup parent) {
// 获取具体的消息对象
final EMMessage message = getItem(position);
// 获得消息类型
ChatType chatType = message.getChatType();
// 生命一个holder
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// 创建该消息的布局
convertView = createViewByMessage(message, position);
if (message.getType() == EMMessage.Type.IMAGE) {
try {
// 消息为图片格式
holder.iv = ((ImageView) convertView.findViewById(R.id.iv_sendPicture));
holder.iv_avatar = (ImageView) convertView.findViewById(R.id.iv_userhead);
holder.tv = (TextView) convertView.findViewById(R.id.percentage);
holder.pb = (ProgressBar) convertView.findViewById(R.id.progressBar);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.tv_usernick = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {
}
} else if (message.getType() == EMMessage.Type.TXT) {
try {
// 消息为文本格式
holder.pb = (ProgressBar) convertView.findViewById(R.id.pb_sending);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.iv_avatar = (ImageView) convertView.findViewById(R.id.iv_userhead);
// 这里是文字内容
holder.tv = (TextView) convertView.findViewById(R.id.tv_chatcontent);
holder.tv_usernick = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {