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) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = layoutInflater.inflate(res, null); holder.avatar = (ImageView) convertView.findViewById(R.id.avatar); holder.unreadMsgView = (TextView) convertView.findViewById(R.id.unread_msg_number); holder.nameTextview = (TextView) convertView.findViewById(R.id.name); holder.tvHeader = (TextView) convertView.findViewById(R.id.header); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } User user = getItem(position); if (user == null) Log.d("ContactAdapter", position + ""); // 设置nick,demo里不涉及到完整user,用username代替nick显示 String username = user.getUsername(); String header = user.getHeader(); if (position == 0 || header != null && !header.equals(getItem(position - 1).getHeader())) { if (TextUtils.isEmpty(header)) { holder.tvHeader.setVisibility(View.GONE); } else { holder.tvHeader.setVisibility(View.VISIBLE); holder.tvHeader.setText(header); } } else { holder.tvHeader.setVisibility(View.GONE); } // 显示申请与通知item if (username.equals(Constant.NEW_FRIENDS_USERNAME)) { holder.nameTextview.setText(user.getNick()); holder.avatar.setImageResource(R.drawable.new_friends_icon); if (user.getUnreadMsgCount() > 0) { holder.unreadMsgView.setVisibility(View.VISIBLE); // holder.unreadMsgView.setText(user.getUnreadMsgCount()+""); } else { holder.unreadMsgView.setVisibility(View.INVISIBLE); } } else if (username.equals(Constant.GROUP_USERNAME)) { // 群聊item holder.nameTextview.setText(user.getNick()); holder.avatar.setImageResource(R.drawable.groups_icon); } else if (username.equals(Constant.CHAT_ROOM)) { // 群聊item holder.nameTextview.setText(user.getNick()); holder.avatar.setImageResource(R.drawable.groups_icon); } else if (username.equals(Constant.CHAT_ROBOT)) { // Robot item holder.nameTextview.setText(user.getNick()); holder.avatar.setImageResource(R.drawable.groups_icon); } else { holder.nameTextview.setText(user.getNick()); // 设置用户头像 UserUtils.setUserAvatar(getContext(), username, holder.avatar); if (holder.unreadMsgView != null) holder.unreadMsgView.setVisibility(View.INVISIBLE); } return convertView; }
@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; }