예제 #1
0
  public int getUnreadMsgCountTotal() {
    int unreadMsgCountTotal = 0;
    int chatroomUnreadMsgCount = 0;
    int chatGroupCount = 0;
    unreadMsgCountTotal = EMChatManager.getInstance().getUnreadMsgsCount();
    for (EMConversation conversation : EMChatManager.getInstance().getAllConversations().values()) {
      if (conversation.getType() == EMConversationType.GroupChat)
        chatroomUnreadMsgCount = chatroomUnreadMsgCount + conversation.getUnreadMsgCount();

      chatGroupCount = chatGroupCount + conversation.getUnreadMsgCount();
    }
    return chatGroupCount;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.row_chat_history, parent, false);
    }
    ViewHolder holder = (ViewHolder) convertView.getTag();
    if (holder == null) {
      holder = new ViewHolder();
      holder.name = (TextView) convertView.findViewById(R.id.name);
      holder.unreadLabel = (TextView) convertView.findViewById(R.id.unread_msg_number);
      holder.message = (TextView) convertView.findViewById(R.id.message);
      holder.time = (TextView) convertView.findViewById(R.id.time);
      holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
      holder.msgState = convertView.findViewById(R.id.msg_state);
      holder.list_item_layout = (RelativeLayout) convertView.findViewById(R.id.list_item_layout);
      convertView.setTag(holder);
    }
    if (position % 2 == 0) {
      holder.list_item_layout.setBackgroundResource(R.drawable.mm_listitem);
    } else {
      holder.list_item_layout.setBackgroundResource(R.drawable.mm_listitem_grey);
    }

    // 获取与此用户/群组的会话
    EMConversation conversation = getItem(position);
    // 获取用户username或者群组groupid
    String username = conversation.getUserName();
    if (conversation.getType() == EMConversationType.GroupChat) {
      // 群聊消息,显示群聊头像
      holder.avatar.setImageResource(R.drawable.group_icon);
      EMGroup group = EMGroupManager.getInstance().getGroup(username);
      holder.name.setText(group != null ? group.getGroupName() : username);
    } else if (conversation.getType() == EMConversationType.ChatRoom) {
      holder.avatar.setImageResource(R.drawable.group_icon);
      EMChatRoom room = EMChatManager.getInstance().getChatRoom(username);
      holder.name.setText(
          room != null && !TextUtils.isEmpty(room.getName()) ? room.getName() : username);
    } else {
      UserUtils.setUserAvatar(getContext(), username, holder.avatar);
      if (username.equals(Constant.GROUP_USERNAME)) {
        holder.name.setText("群聊");

      } else if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
        holder.name.setText("申请与通知");
      }
      Map<String, RobotUser> robotMap =
          ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getRobotList();
      if (robotMap != null && robotMap.containsKey(username)) {
        String nick = robotMap.get(username).getNick();
        if (!TextUtils.isEmpty(nick)) {
          holder.name.setText(nick);
        } else {
          holder.name.setText(username);
        }
      } else {
        UserUtils.setUserNick(username, holder.name);
      }
    }

    if (conversation.getUnreadMsgCount() > 0) {
      // 显示与此用户的消息未读数
      holder.unreadLabel.setText(String.valueOf(conversation.getUnreadMsgCount()));
      holder.unreadLabel.setVisibility(View.VISIBLE);
    } else {
      holder.unreadLabel.setVisibility(View.INVISIBLE);
    }

    if (conversation.getMsgCount() != 0) {
      // 把最后一条消息的内容作为item的message内容
      EMMessage lastMessage = conversation.getLastMessage();
      holder.message.setText(
          SmileUtils.getSmiledText(
              getContext(), getMessageDigest(lastMessage, (this.getContext()))),
          BufferType.SPANNABLE);

      holder.time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
      if (lastMessage.direct == EMMessage.Direct.SEND
          && lastMessage.status == EMMessage.Status.FAIL) {
        holder.msgState.setVisibility(View.VISIBLE);
      } else {
        holder.msgState.setVisibility(View.GONE);
      }
    }

    return convertView;
  }
예제 #3
0
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.layout_item_msg, parent, false);
    }
    ImageView img_avar = ViewHolder.get(convertView, R.id.contactitem_avatar_iv);
    TextView txt_name = ViewHolder.get(convertView, R.id.txt_name);
    TextView txt_state = ViewHolder.get(convertView, R.id.txt_state);
    TextView txt_del = ViewHolder.get(convertView, R.id.txt_del);
    TextView txt_content = ViewHolder.get(convertView, R.id.txt_content);
    TextView txt_time = ViewHolder.get(convertView, R.id.txt_time);
    TextView unreadLabel = ViewHolder.get(convertView, R.id.unread_msg_number);
    SwipeLayout swipe = ViewHolder.get(convertView, R.id.swipe);
    if (PublicMsg != null && position == 0) {
      txt_name.setText("订阅号");
      img_avar.setImageResource(R.drawable.icon_public);
      txt_time.setText(PublicMsg.getTime());
      txt_content.setText(PublicMsg.getContent());
      unreadLabel.setText("3");
      unreadLabel.setVisibility(View.VISIBLE);
      swipe.setSwipeEnabled(false);
    } else {
      swipe.setSwipeEnabled(true);
      // 获取与此用户/群组的会话
      final EMConversation conversation = conversationList.get(position);
      // 获取用户username或者群组groupid
      ChatID = conversation.getUserName();
      txt_del.setTag(ChatID);
      if (conversation.isGroup()) {
        GroupInfo info = GloableParams.GroupInfos.get(ChatID);
        if (info != null) {
          txt_name.setText(info.getGroup_name());
          img_avar.setImageResource(R.drawable.defult_group);
          // initGroupInfo(img_avar, txt_name);// 获取群组信息
        }
      } else {
        User user = GloableParams.Users.get(ChatID);
        if (user != null) {
          txt_name.setText(user.getUserName());
          // initUserInfo(img_avar, txt_name);// 获取用户信息
        }
      }
      if (conversation.getUnreadMsgCount() > 0) {
        // 显示与此用户的消息未读数
        unreadLabel.setText(String.valueOf(conversation.getUnreadMsgCount()));
        unreadLabel.setVisibility(View.VISIBLE);
      } else {
        unreadLabel.setVisibility(View.INVISIBLE);
      }
      if (conversation.getMsgCount() != 0) {
        // 把最后一条消息的内容作为item的message内容
        EMMessage lastMessage = conversation.getLastMessage();
        txt_content.setText(
            SmileUtils.getSmiledText(context, getMessageDigest(lastMessage, context)),
            BufferType.SPANNABLE);
        txt_time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
        if (lastMessage.status == EMMessage.Status.SUCCESS) {
          txt_state.setText("送达");
          // txt_state.setBackgroundResource(R.drawable.btn_bg_orgen);
        } else if (lastMessage.status == EMMessage.Status.FAIL) {
          txt_state.setText("失败");
          // txt_state.setBackgroundResource(R.drawable.btn_bg_red);
        } else if (lastMessage.direct == EMMessage.Direct.RECEIVE) {
          txt_state.setText("已读");
          txt_state.setBackgroundResource(R.drawable.btn_bg_blue);
        }
      }

      txt_del.setOnClickListener(
          new OnClickListener() {

            @Override
            public void onClick(View v) {
              deleteID = position;
              Tipdialog = new WarnTipDialog((Activity) context, "您确定要删除该聊天吗?");
              Tipdialog.setBtnOkLinstener(onclick);
              Tipdialog.show();
            }
          });
    }
    return convertView;
  }