/** * 设置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; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ChatUser o = (ChatUser) obj; if (getUserName().equals(o.getUserName()) && getUserId().equals(o.getUserId()) && getUserFeel().equals(o.getUserFeel()) && getImgPath().equals(o.getImgPath()) && getGroupId().equals(o.getGroupId()) && getChatTime().equals(o.getChatTime())) return true; return super.equals(obj); }
@Override protected synchronized FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); if (mOriginalList == null) { mOriginalList = new ArrayList<ChatUser>(); } EMLog.d(TAG, "contacts original size: " + mOriginalList.size()); EMLog.d(TAG, "contacts copy size: " + copyUserList.size()); if (prefix == null || prefix.length() == 0) { results.values = copyUserList; results.count = copyUserList.size(); } else { String prefixString = prefix.toString(); final int count = mOriginalList.size(); final ArrayList<ChatUser> newValues = new ArrayList<ChatUser>(); for (int i = 0; i < count; i++) { final ChatUser user = mOriginalList.get(i); String username = user.getUserName(); if (username.startsWith(prefixString)) { newValues.add(user); } else { final String[] words = username.split(" "); final int wordCount = words.length; // Start at index 0, in case valueText starts with space(s) for (int k = 0; k < wordCount; k++) { if (words[k].startsWith(prefixString)) { newValues.add(user); break; } } } } results.values = newValues; results.count = newValues.size(); } EMLog.d(TAG, "contacts filter results size: " + results.count); return results; }