for (Message message : vector) {
String from = message.getFrom();
String body = message.getBody();
LogUtil.i("ShowGroupChatMessage", from + ":" + body);
// 判断是我说的,还是别人说的
View view = null;
if (from.equals(TApplication.currentUser.getUser())) {
// 我说的
view = View.inflate(context, R.layout.right, null);
} else {
// 好友说的
view = View.inflate(context, R.layout.left, null);
}
if (!from.equals(TApplication.currentUser.getUser())) {
// 好友说的
TextView tvFriendName = (TextView) view
.findViewById(R.id.tv_chat_friendName);
// from:
[email protected]/friendName
String friendName = from.substring(from
.lastIndexOf("/") + 1);
tvFriendName.setText(friendName);
}
// 显示,判断数据的类型,有文本,有face
// 不同类型显示不一样
int type = ChatUtil.getType(body);
if (type == ChatUtil.TYPE_AUDIO) {//是语言
ImageView ivPlay = (ImageView) view
.findViewById(R.id.iv_playAudio);
ivPlay.setVisibility(View.VISIBLE);
ivPlay.setTag(body);
ivPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
String body = (String) v.getTag();
ChatUtil.getAudio(body);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(ChatUtil
.getAudioFile().getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
});
}
if (type == ChatUtil.TYPE_IMAGE) {//图片
ImageView iv = (ImageView) view
.findViewById(R.id.iv_chat);
iv.setVisibility(View.VISIBLE);
iv.setImageBitmap(ChatUtil.getImage(body));
}
if (type == ChatUtil.TYPE_TEXT) {//文本
TextView tv = (TextView) view
.findViewById(R.id.tv_chat_text);
tv.setVisibility(View.VISIBLE);
tv.setText(body);
}
if (type == ChatUtil.TYPE_FACE) {//表情
GifImageView gifImageView = (GifImageView) view
.findViewById(R.id.gifImageView);
gifImageView.setVisibility(View.VISIBLE);
String fileName = ChatUtil.getFaceFileName(body);
GifDrawable gifDrawable = new GifDrawable(getAssets(),
fileName);
gifImageView.setBackgroundDrawable(gifDrawable);
}
llChatContent.addView(view);
vector.remove(message);
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
// 向上移
// int linearLayoutHeight = llChatContent
// .getHeight();
// int scrollViewHeight =
// scrollView.getHeight();
// LogUtil.i("向上移", "linearLayoutHeight="
// + linearLayoutHeight
// + " scrollViewHeight="
// + scrollViewHeight);
//
// if (linearLayoutHeight > scrollViewHeight) {
// int moveY = linearLayoutHeight
// - scrollViewHeight;
// scrollView.scrollTo(0, moveY);
// }
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
} catch (Exception e) {
// TODO: handle exception
}
}
}, 1);// 有的手机samsung s4时间要长一点
}
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
}