android 通讯录 首字母索引,android仿微信通讯录搜索(匹配拼音,字母,索引位置标记颜色)...
前言:
仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置
一:先看效果图
字母索引
搜索匹配
二:功能分析
1:汉字转拼音
通讯录汉字转拼音(首个字符当考虑姓氏多音字), 现在转换拼音常见的有pinyin4j和tinypinyin, pinyin4j的功能强大,包含声调多音字,tinypinyin执行快占用内存少, 如果只是简单匹配通讯录,建议使用tinypinyin,用法也很简单这里不详细介绍
拼音类
public class CNPinyin implements Serializable, Comparable> {
/**
* 对应首字首拼音字母
*/
char firstChar;
/**
* 所有字符中的拼音首字母
*/
String firstChars;
/**
* 对应的所有字母拼音
*/
String[] pinyins;
/**
* 拼音总长度
*/
int pinyinsTotalLength;
public final T data;
CNPinyin(T data) {
this.data = data;
}
public char getFirstChar() {
return firstChar;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder().append("--firstChar--").append(firstChar).append("--pinyins:");
for (String str : pinyins) {
sb.append(str);
}
return sb.toString();
}
int compareValue() {
if (firstChar == DEF_CHAR) {
return 'Z' + 1;
}
return firstChar;
}
@Override
public int compareTo(CNPinyin tcnPinyin) {
int compare = compareValue() - tcnPinyin.compareValue();
if (compare == 0) {
String chinese1 = data.chinese();
String chinese2 = tcnPinyin.data.chinese();
return chinese1.compareTo(chinese2);
}
return compare;
}
}
2:定义索引栏 a~z,#控件
ItemDecoration配合RecyclerView实现StickyHeader效果,此效果很常见不详细介绍
3:根据转换好的拼音快速匹配
搜索匹配才是核心, 以下匹配原则,有优先顺序如果有匹配成功不执行后面的匹配原则
a:匹配原字符 并找出所匹配的起始位置与结束位置,如有中文匹配将不执行后面的拼音匹配原则
static CNPinyinIndex matcherChinese(CNPinyin cnPinyin, String keyword) {
if (keyword.length() < cnPinyin.data.chinese().length()) {
Matcher matcher = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(cnPinyin.data.chinese());
if (matcher.find()) {
return new CNPinyinIndex(cnPinyin, matcher.start(), matcher.end());
}
}
return null;
}
b:匹配单个字符拼音的首个字母(例如"游小陈"可以匹配y, x, c, yx, xc, yxc)
static CNPinyinIndex matcherFirst(CNPinyin cnPinyin, String keyword) {
if (keyword.length() <= cnPinyin.pinyins.length) {
Matcher matcher = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(cnPinyin.firstChars);
if (matcher.find()) {
return new CNPinyinIndex(cnPinyin, matcher.start(), matcher.end());
}
}
return null;
}
c:所有字符拼音的匹配, 且第一个匹配位置的拼音必须一致(例如"游小陈 youxiaochen", 必须匹配yo, you, xi, xia, xiao, ch, che, chen开头等 例如 yo youx, youxi, youxiao, xiaoc, xiaoch, xiaochen等等)
/**
* 所有拼音匹配
* @param cnPinyin
* @param keyword
* @return
*/
static CNPinyinIndex matchersPinyins(CNPinyin cnPinyin, String keyword) {
if (keyword.length() > cnPinyin.pinyinsTotalLength) return null;
int start = -1;
int end = -1;
for (int i = 0; i < cnPinyin.pinyins.length; i++) {
String pat = cnPinyin.pinyins[i];
if (pat.length() >= keyword.length()) {//首个位置索引
Matcher matcher = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(pat);
if (matcher.find() && matcher.start() == 0) {
start = i;
end = i + 1;
break;
}
} else {
Matcher matcher = Pattern.compile(pat, Pattern.CASE_INSENSITIVE).matcher(keyword);
if (matcher.find() && matcher.start() == 0) {//全拼匹配第一个必须在0位置
start = i;
String left = matcher.replaceFirst("");
end = end(cnPinyin.pinyins, left, ++i);
break;
}
}
}
if (start >= 0 && end >= start) {
return new CNPinyinIndex(cnPinyin, start, end);
}
return null;
}
/**
* 根据匹配字符递归查找下一结束位置
* @param pinyinGroup
* @param pattern
* @param index
* @return -1 匹配失败
*/
private static int end(String[] pinyinGroup, String pattern, int index) {
if (index < pinyinGroup.length) {
String pinyin = pinyinGroup[index];
if (pinyin.length() >= pattern.length()) {//首个位置索引
Matcher matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(pinyin);
if (matcher.find() && matcher.start() == 0) {
return index + 1;
}
} else {
Matcher matcher = Pattern.compile(pinyin, Pattern.CASE_INSENSITIVE).matcher(pattern);
if (matcher.find() && matcher.start() == 0) {//全拼匹配第一个必须在0位置
String left = matcher.replaceFirst("");
return end(pinyinGroup, left, index + 1);
}
}
}
return -1;
}
android 通讯录 首字母索引,android仿微信通讯录搜索(匹配拼音,字母,索引位置标记颜色)...相关推荐
- RecyclerView+index索引实现仿微信通讯录
感觉之前写的有点乱,所以有重新整理了一下这个博客: demo下载地址:http://download.csdn.net/detail/qq_34501274/9799175 最近跟朋友聊天,说道博客相 ...
- 【uniapp前端组件】仿微信通讯录列表组件
仿微信通讯录列表组件 示例图 前言 仿微信通讯录列表组件,可实现通讯列表以及选择多个联系人功能. 组件介绍 本组件有三个自定义组件构成,都已经集成在bugking7-contact-list中,该组件 ...
- android 字母搜索栏,android仿微信通讯录搜索示例(匹配拼音,字母,索引位置)
前言: 仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置 一:先看效果图 字母索引 搜索匹配 二:功能分析 1:汉字转拼音 通讯录汉字转拼音(首个字符当考虑姓氏多音字), 现 ...
- Android仿微信通讯录
Android仿微信通讯录 分3部: 1.listview实现显示头像.名字(太简单,这里就不写了) 通讯录页面xml布局代码: <LinearLayout xmlns:android=&quo ...
- 【Android 仿微信通讯录 导航分组列表-上】使用ItemDecoration为RecyclerView打造带悬停头部的分组列表
*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52355199 本文 ...
- uni-app 写小程序 索引列表,仿微信通讯录
心里认定了一个女孩 就要好好的珍惜对待她,人生不容辜负,你必须要更加努力 .加油 骚年 uni-app 写小程序 索引列表,仿微信通讯录 去uni-app官网 下载插件 indexlist <m ...
- 仿微信通讯录的Demo----PinnedHeaderListView
仿微信通讯录的Demo--PinnedHeaderListView 侧边栏首字母匹配 + header分组 本示例代码来自网上 这里只贴出效果图,附件Demo源码,仅供学习和以后参考时用 附件 Dem ...
- 【Android 仿微信通讯录 导航分组列表-下】自定义View为RecyclerView打造右侧索引导航栏IndexBar
本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52420706 本文出 ...
- Android实现首字母导航条(仿微信)
本博客介绍Android实现首字母导航条,先看张效果图,具体怎么实现看代码吧 具体的步骤 1.整体布局的显示 2. 实现A-Z的分组 3. 自定义A-Z的导航条 4. 中间显示/隐藏触摸到导航条具体的 ...
最新文章
- 什么是事务的传播_这么漂亮的Spring事务管理详解,你不来看看?
- 【笔记】大数定理证明
- 报告称苹果正扩展CareKit 组建团队推动医疗数据数字化
- 【Java】命令行下的Java模块化建构、编译与执行
- Vue.Draggable拖拽功能的配置和使用方法
- Java中的包扫描(工具)
- php 建站要学,建站新手如何开始学习php?
- 第二章 身份验证——《跟我学Shiro》[张开涛]
- FPGA零基础学习:SDR SDRAM 驱动设计
- 计算机运行黑屏显示器正常,电脑主机运行正常显示器黑屏怎么办
- 隐性代言vancl 徐静蕾卖的不只是项链?
- win10用不了计算机一级,教你一招解决Win10计算器打不开的问题
- Java 多态练习题之愤怒的小鸟,会飞会叫
- Python特定场景数据内型“解压”操作
- 上海科学家研制出新型“耐火宣纸”
- 项目管理过程-5个管理过程组、10大管理知识域以及对应输入、工具技术和输出
- 数据不满足正态分布,方差齐性怎么办?
- ArcMap符号样式制作
- maven 依赖com.google.code.kaptcha
- Jmeter TCP 取样器 上一个请求响应结果参数传个下一个请求,响应结果为空
热门文章
- 阿里云服务器几分钟快速建站
- 论文阅读笔记:GraphRAD---A Graph-based Risky Account Detection System
- 《营在微博:企业微博营销实战宝典(全彩精印)》图书信息
- ddr最大工作频率 xc7z020_米尔科技XC7Z020开发板介绍
- CF449B Jzzhu and Cities
- Excel里面经纬度度分秒转换成度
- win7计划任务打开历史记录
- 读研究生与蹲监狱的联系和区别
- 解析淘宝淘口令获取商品ID/uland链接e参数获取返回商品优惠券方法说明
- Could not locate executable null\bin\winutils.exe in the Hadoop binaries全网最强windows10安装hadoop教程