- Support sidebar index
- Support Chinese, number, letter fuzzy search
- Support all select, single select contacts
- Support to check whether the contact has been added
Single choice demo
Multiple choice demo
Function analysis
Most of the code is relatively simple, so I won't paste it here and take up space. I only analyze a few technical points. You can directly download the source code to observe.
The first question: Chinese characters to pinyin
Depends on tinypinyin library
implementation 'com.github.promeg:tinypinyin:2.0.3'
- 1
When you get the first Chinese character and pass it in, it will be automatically converted into pinyin, and then take the first character of pinyin.
Pinyin.toPinyin(name.charAt(0)).toUpperCase().charAt(0)
- 1
Second question: how to sort contacts from AZ
In the first question, we got the first letter of each contact, and sorted the contacts in the loop in ascending order through the compareTo of the Comparator
/**
* 把联系人按照a b c升序排列
*/
private static ArrayList<ContactInfo> compare(ArrayList<ContactInfo> contactInfos) {
Collections.sort(contactInfos, new Comparator<ContactInfo>() {
@Override
public int compare(ContactInfo o1, ContactInfo o2) {
//升序排列
if (o1.getLetter().equals("@")
|| o2.getLetter().equals("#")) {
return -1;
} else if (o1.getLetter().equals("#")
|| o2.getLetter().equals("@")) {
return 1;
}
return o1.getLetter().compareTo(o2.getLetter());
}
});
return contactInfos;
}
The third question: how to display the classification letters of contacts by alphabet
In the second question, we arranged the contacts in ascending alphabetical order. Here we only need to determine whether the first letters of the contacts are equal, that is, if the first letter of the first contact and the second contact are different, it means that they are not of the same type , This is to display the index letter on the second contact.
public void onBindViewHolder(ContactAdapter.MyViewHodle holder, final int position) {
ContactInfo contactInfo = mContactList.get(position);
holder.name.setText(contactInfo.getName());
holder.phone.setText(contactInfo.getPhone());
//判断是否显示索引字母
String currentLetter = contactInfo.getLetter();
String previousLetter = position >= 1 ? mContactList.get(position - 1).getLetter() : "";
if (!TextUtils.equals(currentLetter, previousLetter)) {
holder.letter.setVisibility(View.VISIBLE);
holder.letter.setText(currentLetter);
} else {
holder.letter.setVisibility(View.GONE);
}
.....省略.....
}
The fourth question: How to search for contacts
Get all the contacts first, and then search again, put the searched contacts into another collection, and then send it to RecycleView to notify the refresh
/**
* 搜索联系人
*
* @param searchKey 搜索key
*/
private void searchContacts(String searchKey) {
for (ContactInfo info : mContactList) {
if (ContactsUtils.searchContact(searchKey, info)) {
mSearchList.add(info);
}
}
}
/**
* 模糊搜索(按中文,数字,字母搜索)
*/
public static boolean searchContact(String searchStr, ContactInfo info) {
return info.getName().contains(searchStr) || info.getPhone().contains(searchStr)
|| searchLowerByAlphabet(searchStr, info) || searchUpperByAlphabet(searchStr, info)
|| Pinyin.toPinyin(info.getName(), "").toLowerCase().contains(searchStr)
|| Pinyin.toPinyin(info.getName(), "").toUpperCase().contains(searchStr);
}
The fourth question: how to choose a contact
Determine whether to select a contact through the isChooseContact field
The fourth question: How does the contact judge whether it has been added
Determine whether a contact has been added through the isAddContact field