/** * 设置hearder属性,方便通讯中对联系人按header分类显示,以及通过右侧ABCD...字母栏快速定位联系人 * * @param username * @param user */ public void setUserHearder(String username, ChatUser user) { String headerName = null; if (!TextUtils.isEmpty(user.getNick())) { headerName = user.getNick(); } else { headerName = user.getUserName(); } if (username.equals(Constant.NEW_FRIENDS_USERNAME)) { user.setHeader(""); } else if (Character.isDigit(headerName.charAt(0))) { user.setHeader("#"); } else { user.setHeader( HanziToPinyin.getInstance() .get(headerName.substring(0, 1)) .get(0) .target .substring(0, 1) .toUpperCase()); char header = user.getHeader().toLowerCase().charAt(0); if (header < 'a' || header > 'z') { user.setHeader("#"); } } }
@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(); } ChatUser 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 ("".equals(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 { holder.nameTextview.setText(username); // 设置用户头像 // UserUtils.setUserAvatar(getContext(), username, holder.avatar); if (holder.unreadMsgView != null) holder.unreadMsgView.setVisibility(View.INVISIBLE); } return convertView; }