标签:
// 这个地方跳转一定要快然后分析openChatActivity()函数。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
RecentInfo recentInfo = contactAdapter.getItem(position);
if (recentInfo == null) {
logger.e("recent#null recentInfo -> position:%d", position);
return;
}
IMUIHelper.openChatActivity(getActivity(),recentInfo.getSessionKey());
}
// 跳转到聊天页面至此,我们跳转到了对话界面。
public static void openChatActivity(Context ctx, String sessionKey) {
Intent intent = new Intent(ctx, MessageActivity.class);
intent.putExtra(IntentConstant.KEY_SESSION_KEY, sessionKey);
ctx.startActivity(intent);
}
case R.id.send_message_btn: {并且,程序能够正常显示发送内容。
logger.d("message_activity#send btn clicked");
String content = messageEdt.getText().toString();
logger.d("message_activity#chat content:%s", content);
if (content.trim().equals("")) {
Toast.makeText(MessageActivity.this,
getResources().getString(R.string.message_null), Toast.LENGTH_LONG).show();
return;
}
TextMessage textMessage = TextMessage.buildForSend(content, loginUser, peerEntity);
imService.getMessageManager().sendText(textMessage);
messageEdt.setText("");
pushList(textMessage);
scrollToBottomListItem();
}
public void pushList(MessageEntity msg) {查看addItem()函数如下
logger.d("chat#pushList msgInfo:%s", msg);
adapter.addItem(msg);
}
/**在看scrollToBottomListItem() 函数
* ----------------------添加历史消息-----------------
*/
public void addItem(final MessageEntity msg) {
if (msg.getDisplayType() == DBConstant.MSG_TYPE_SINGLE_TEXT) {
if (isMsgGif(msg)) {
msg.setGIfEmo(true);
} else {
msg.setGIfEmo(false);
}
}
int nextTime = msg.getCreated();
if (getCount() > 0) {
Object object = msgObjectList.get(getCount() - 1);
if (object instanceof MessageEntity) {
int preTime = ((MessageEntity) object).getCreated();
boolean needTime = DateUtil.needDisplayTime(preTime, nextTime);
if (needTime) {
Integer in = nextTime;
msgObjectList.add(in);
}
}
} else {
Integer in = msg.getCreated();
msgObjectList.add(in);
}
/**消息的判断*/
if (msg.getDisplayType() == DBConstant.SHOW_MIX_TEXT) {
MixMessage mixMessage = (MixMessage) msg;
msgObjectList.addAll(mixMessage.getMsgList());
} else {
msgObjectList.add(msg);
}
if (msg instanceof ImageMessage) {
ImageMessage.addToImageMessageList((ImageMessage) msg);
}
logger.d("#messageAdapter#addItem");
notifyDataSetChanged();
}
/**
* @Description 滑动到列表底部
*/
private void scrollToBottomListItem() {
logger.d("message_activity#scrollToBottomListItem");
// todo eric, why use the last one index + 2 can real scroll to the
// bottom?
ListView lv = lvPTR.getRefreshableView();
if (lv != null) {
lv.setSelection(adapter.getCount() + 1);
}
textView_new_msg_tip.setVisibility(View.GONE);
}
public void loadHistoryList(final List<MessageEntity> historyList) {
logger.d("#messageAdapter#loadHistoryList");
if (null == historyList || historyList.size() <= 0) {
return;
}
Collections.sort(historyList, new MessageTimeComparator());
ArrayList<Object> chatList = new ArrayList<>();
int preTime = 0;
int nextTime = 0;
for (MessageEntity msg : historyList) {
if (msg.getDisplayType() == DBConstant.MSG_TYPE_SINGLE_TEXT) {
if (isMsgGif(msg)) {
msg.setGIfEmo(true);
} else {
msg.setGIfEmo(false);
}
}
nextTime = msg.getCreated();
boolean needTimeBubble = DateUtil.needDisplayTime(preTime, nextTime);
if (needTimeBubble) {
Integer in = nextTime;
chatList.add(in);
}
preTime = nextTime;
if (msg.getDisplayType() == DBConstant.SHOW_MIX_TEXT) {
MixMessage mixMessage = (MixMessage) msg;
chatList.addAll(mixMessage.getMsgList());
} else {
chatList.add(msg);
}
}
// 如果是历史消息,从头开始加
msgObjectList.addAll(0, chatList);
getImageList();
logger.d("#messageAdapter#addItem");
notifyDataSetChanged();
}
private void initData() {其中,初始化消息的函数是:reqHistoryMsg();
historyTimes = 0;
adapter.clearItem();
ImageMessage.clearImageMessageList();
loginUser = imService.getLoginManager().getLoginInfo();
peerEntity = imService.getSessionManager().findPeerEntity(currentSessionKey);
// 头像、历史消息加载、取消通知
setTitleByUser();
reqHistoryMsg();
adapter.setImService(imService, loginUser);
imService.getUnReadMsgManager().readUnreadSession(currentSessionKey);
imService.getNotificationManager().cancelSessionNotifications(currentSessionKey);
}
/**在看pushList()函数:
* 1.初始化请求历史消息
* 2.本地消息不全,也会触发
*/
private void reqHistoryMsg() {
historyTimes++;
List<MessageEntity> msgList = imService.getMessageManager().loadHistoryMsg(historyTimes,currentSessionKey,peerEntity);
pushList(msgList);
scrollToBottomListItem();
}
public void pushList(List<MessageEntity> entityList) {在reqHistoryMsg()中加入调试打印出相关信息。
logger.d("chat#pushList list:%d", entityList.size());
adapter.loadHistoryList(entityList);
}
private void reqHistoryMsg() {
historyTimes++;
List<MessageEntity> msgList = imService.getMessageManager().loadHistoryMsg(historyTimes,currentSessionKey,peerEntity);
Log.d("messageActivity","size "+msgList.size()+ " list "+msgList.toString()+"imservice"+imService.toString());
pushList(msgList);
scrollToBottomListItem();
}
标签:
原文地址:http://www.cnblogs.com/yuqt/p/5486019.html