Ejemplo n.º 1
0
 private void initListener() {
   Intent intent = getIntent();
   String username = intent.getStringExtra("username");
   boolean enableUpdate = intent.getBooleanExtra("setting", false);
   if (enableUpdate) {
     headPhotoUpdate.setVisibility(View.VISIBLE);
     iconRightArrow.setVisibility(View.VISIBLE);
     rlNickName.setOnClickListener(this);
     headAvatar.setOnClickListener(this);
   } else {
     headPhotoUpdate.setVisibility(View.GONE);
     iconRightArrow.setVisibility(View.INVISIBLE);
   }
   if (username == null) {
     tvUsername.setText(EMChatManager.getInstance().getCurrentUser());
     UserUtils.setCurrentUserNick(tvNickName);
     UserUtils.setCurrentUserAvatar(this, headAvatar);
   } else if (username.equals(EMChatManager.getInstance().getCurrentUser())) {
     tvUsername.setText(EMChatManager.getInstance().getCurrentUser());
     UserUtils.setCurrentUserNick(tvNickName);
     UserUtils.setCurrentUserAvatar(this, headAvatar);
   } else {
     tvUsername.setText(username);
     UserUtils.setUserNick(username, tvNickName);
     UserUtils.setUserAvatar(this, username, headAvatar);
     asyncFetchUserInfo(username);
   }
 }
  @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;
  }