【转】仿Android 联系人SideBar排序,根据拼音A-Z字母快速导航,以及输入搜索条件过滤,显示姓名的文字图片
1.首先我们把这几个工具类拷贝到自己的项目中,这些都是很常见的类:
CharacterParser –这是用来把中文转成拼音的工具类
PinyinComparator –拼音首字母的比较器
SideBar –右侧的竖条,显示的是二十六个字母以及*,和#号
SortModel –放排序name和key的bean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class CharacterParser { 2 private static int[] pyvalue = new int[] { -20319, -20317, -20304, -20295, 3 -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, 4 -20032, -20026, -20002, -19990, -19986, -19982, -19976, -19805, 5 -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, 6 -19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515, 7 -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, 8 -19270, -19263, -19261, -19249, -19243, -19242, -19238, -19235, 9 -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, 10 -19003, -18996, -18977, -18961, -18952, -18783, -18774, -18773, 11 -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, 12 -18696, -18526, -18518, -18501, -18490, -18478, -18463, -18448, 13 -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, 14 -18184, -18183, -18181, -18012, -17997, -17988, -17970, -17964, 15 -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, 16 -17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683, 17 -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, 18 -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, 19 -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, 20 -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, 21 -16419, -16412, -16407, -16403, -16401, -16393, -16220, -16216, 22 -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, 23 -16155, -15959, -15958, -15944, -15933, -15920, -15915, -15903, 24 -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, 25 -15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435, 26 -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, 27 -15363, -15362, -15183, -15180, -15165, -15158, -15153, -15150, 28 -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, 29 -15119, -15117, -15110, -15109, -14941, -14937, -14933, -14930, 30 -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, 31 -14894, -14889, -14882, -14873, -14871, -14857, -14678, -14674, 32 -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, 33 -14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345, 34 -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, 35 -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, 36 -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, 37 -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, 38 -13831, -13658, -13611, -13601, -13406, -13404, -13400, -13398, 39 -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, 40 -13340, -13329, -13326, -13318, -13147, -13138, -13120, -13107, 41 -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, 42 -12875, -12871, -12860, -12858, -12852, -12849, -12838, -12831, 43 -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, 44 -12359, -12346, -12320, -12300, -12120, -12099, -12089, -12074, 45 -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, 46 -11781, -11604, -11589, -11536, -11358, -11340, -11339, -11324, 47 -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, 48 -11038, -11024, -11020, -11019, -11018, -11014, -10838, -10832, 49 -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, 50 -10519, -10331, -10329, -10328, -10322, -10315, -10309, -10307, 51 -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254 }; 52 public static String[] pystr = new String[] { "a", "ai", "an", "ang", "ao", 53 "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", 54 "bian", "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", 55 "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", 56 "chao", "che", "chen", "cheng", "chi", "chong", "chou", "chu", 57 "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", 58 "cou", "cu", "cuan", "cui", "cun", "cuo", "da", "dai", "dan", 59 "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", 60 "diu", "dong", "dou", "du", "duan", "dui", "dun", "duo", "e", "en", 61 "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", 62 "ga", "gai", "gan", "gang", "gao", "ge", "gei", "gen", "geng", 63 "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", 64 "guo", "ha", "hai", "han", "hang", "hao", "he", "hei", "hen", 65 "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", 66 "hun", "huo", "ji", "jia", "jian", "jiang", "jiao", "jie", "jin", 67 "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", 68 "kan", "kang", "kao", "ke", "ken", "keng", "kong", "kou", "ku", 69 "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", 70 "lan", "lang", "lao", "le", "lei", "leng", "li", "lia", "lian", 71 "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", 72 "lv", "luan", "lue", "lun", "luo", "ma", "mai", "man", "mang", 73 "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", 74 "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", "nan", 75 "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", 76 "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", 77 "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", 78 "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", 79 "pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", 80 "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", 81 "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", 82 "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", 83 "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", 84 "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", 85 "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", 86 "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", 87 "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", 88 "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", 89 "weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", "xie", 90 "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", 91 "yan", "yang", "yao", "ye", "yi", "yin", "ying", "yo", "yong", 92 "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", 93 "zao", "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang", 94 "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", 95 "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi", 96 "zong", "zou", "zu", "zuan", "zui", "zun", "zuo" }; 97 private StringBuilder buffer; 98 private String resource; 99 private static CharacterParser characterParser = new CharacterParser(); 100 101 public static CharacterParser getInstance() { 102 return characterParser; 103 } 104 105 public String getResource() { 106 return resource; 107 } 108 109 public void setResource(String resource) { 110 this.resource = resource; 111 } 112 113 /** * 汉字转成ASCII* * @param chs * @return */ 114 private int getChsAscii(String chs) { 115 int asc = 0; 116 try { 117 byte[] bytes = chs.getBytes("gb2312"); 118 if (bytes == null || bytes.length > 2 || bytes.length <= 0) { 119 throw new RuntimeException("illegal resource string"); 120 } 121 if (bytes.length == 1) { 122 asc = bytes[0]; 123 } 124 if (bytes.length == 2) { 125 int hightByte = 256 + bytes[0]; 126 int lowByte = 256 + bytes[1]; 127 asc = (256 * hightByte + lowByte) - 256 * 256; 128 } 129 } catch (Exception e) { 130 System.out 131 .println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" 132 + e); 133 } 134 return asc; 135 } 136 137 /** * 单字解析 * * @param str * @return */ 138 public String convert(String str) { 139 String result = null; 140 int ascii = getChsAscii(str); 141 if (ascii > 0 && ascii < 160) { 142 result = String.valueOf((char) ascii); 143 } else { 144 for (int i = (pyvalue.length - 1); i >= 0; i--) { 145 if (pyvalue[i] <= ascii) { 146 result = pystr[i]; 147 break; 148 } 149 } 150 } 151 return result; 152 } 153 154 /** * 词组解析 * * @param chs * @return */ 155 public String getSelling(String chs) { 156 String key, value; 157 buffer = new StringBuilder(); 158 for (int i = 0; i < chs.length(); i++) { 159 key = chs.substring(i, i + 1); 160 if (key.getBytes().length >= 2) { 161 value = (String) convert(key); 162 if (value == null) { 163 value = "unknown"; 164 } 165 } else { 166 value = key; 167 } 168 buffer.append(value); 169 } 170 return buffer.toString(); 171 } 172 173 public String getSpelling() { 174 return this.getSelling(this.getResource()); 175 } 176 177 }