• tt程序分析(一)


    首先是loginactivity
    login成功以后,跳转到mainActivity。
    mainActivity中有四个fragment ,
    聊天        fragment_chat
    通讯录    fragment_contact
    发现,    fragment_internal
    我的       fragment_my
     
     
    在聊天fragment_chat中,设置点击每个item,可以跳转到相应的页面
    fragement_chat.java 代码:
    // 这个地方跳转一定要快
    @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());
    }
    然后分析openChatActivity()函数。
    IMUIHelper.java 中代码如下:
    // 跳转到聊天页面
    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();
    }
    并且,程序能够正常显示发送内容。
    看pushList()函数如下
    public void pushList(MessageEntity msg) {
    logger.d("chat#pushList msgInfo:%s", msg);
    adapter.addItem(msg);
    }
    查看addItem()函数如下
    /**
    * ----------------------添加历史消息-----------------
    */
    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();

    }
    在看scrollToBottomListItem() 函数
    /**
    * @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);
    }
    消息添加到msgObjgecList中,并且可以显示。
    msgObject的初始化
    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();
    }
     
    在看对对话界面的初始化:
     
    在MessageFragment.java
    初始化数据:
    private void initData() {
    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);
    }
    其中,初始化消息的函数是:reqHistoryMsg();
    /**
    * 1.初始化请求历史消息
    * 2.本地消息不全,也会触发
    */
    private void reqHistoryMsg() {
    historyTimes++;
    List<MessageEntity> msgList = imService.getMessageManager().loadHistoryMsg(historyTimes,currentSessionKey,peerEntity);
    pushList(msgList);
    scrollToBottomListItem();
    }
    在看pushList()函数:
    public void pushList(List<MessageEntity> entityList) {
    logger.d("chat#pushList list:%d", entityList.size());
    adapter.loadHistoryList(entityList);
    }
    在reqHistoryMsg()中加入调试打印出相关信息。
    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();
    }
     
    05-12 15:42:41.524: D/messageActivity(26765): 
     
     
  • 相关阅读:
    UVA——A Spy in the Metro(线性dp)
    IDEA运行jsp文件变成源码详细解决方案
    CF1105C Ayoub and Lost Array(dp+矩阵快速幂优化)
    牛客练习赛75——A广义肥波
    void * 指针和const 指针
    详解getchar()函数与缓冲区
    深入了解scanf() getchar()和gets()等函数之间的区别
    字符串和指针注意
    指针
    数组和字符串的小结
  • 原文地址:https://www.cnblogs.com/yuqt/p/5486019.html
Copyright © 2020-2023  润新知