对于通讯录的实现相信大家基本都实现过了,我这里也就记录一下以前实现的过程中遇到的一些问题,以及记录一下Demo的代码,方便以后用到的时候可以快速的查看,编写。效果图如下:

对于列表的数据已字母的顺序排列,右边的侧滑菜单,可以实现滑动的时候,侧滑菜单的背景色变成灰色,当前的列表对应的字母变为蓝色、通讯录列表也跟着sideBar的滑动做上下的滑动,具体的效果,可以运行Demo的时候研究。

  • 对汉字的排序
  • 根据字母对列表分类
  • 写滑动事件

对汉字的排序

对汉字的排序主要分为先两步:一步,将汉字转化为拼音。第二步,根据拼音排序。将汉字转化为拼音是用的一个工具类,这个网上有很多,原理就是先将汉字转化为ASCll码,在与预先写好的常用拼音的ASCII码做比较,判断范围,如果需要转化的汉字的ASCII码在预先保存拼音的ASCII码中,就认为改汉字的拼音为此拼音。

package lovelder.lovcreate.com.sidebarapplication;/*** 中文转拼音转换类* @author J**/
public class CharacterParser {private static int[] pyvalue = new int[] {-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032,-20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,-19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261,-19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961,-18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490,-18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988,-17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692,-17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733,-16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419,-16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,-15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631,-15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180,-15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941,-14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857,-14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353,-14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094,-14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831,-13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,-13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860,-12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300,-12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358,-11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014,-10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309,-10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254};public static String[] pystr = new String[] {"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian","biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che","chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan","cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du","duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang","gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang","hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian","jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken","keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng","li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai","man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai","nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan","nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu","qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re","ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha","shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun","shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao","tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi","xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi","yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha","zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui","zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"};private StringBuilder buffer;private String resource;private static CharacterParser characterParser = new CharacterParser();public static CharacterParser getInstance() {return characterParser;}public String getResource() {return resource;}public void setResource(String resource) {this.resource = resource;}private int getChsAscii(String chs) {int asc = 0;try {byte[] bytes = chs.getBytes("gb2312");if (bytes == null || bytes.length > 2 || bytes.length <= 0) {throw new RuntimeException("illegal resource string");}if (bytes.length == 1) {asc = bytes[0];}if (bytes.length == 2) {int hightByte = 256 + bytes[0];int lowByte = 256 + bytes[1];asc = (256 * hightByte + lowByte) - 256 * 256;}} catch (Exception e) {System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e);}return asc;}public String convert(String str) {String result = null;int ascii = getChsAscii(str);if (ascii > 0 && ascii < 160) {result = String.valueOf((char) ascii);} else {for (int i = (pyvalue.length - 1); i >= 0; i--) {if (pyvalue[i] <= ascii) {result = pystr[i];break;}}}return result;}public String getSelling(String chs) {String key, value;buffer = new StringBuilder();for (int i = 0; i < chs.length(); i++) {key = chs.substring(i, i + 1);if (key.getBytes().length >= 2) {value = (String) convert(key);if (value == null) {value = "unknown";}} else {value = key;}buffer.append(value);}return buffer.toString();}public String getSpelling() {return this.getSelling(this.getResource());}}

当把汉字转化为拼音后,就可以用Java自带的比较器对拼音做一个比较,比较器的代码如下:

package lovelder.lovcreate.com.sidebarapplication;
import java.util.Comparator;
/*** 作者:yuanYe创建于2016/9/26* QQ:962851730*/
public class PinyinComparator implements Comparator<AddressListBean> {public int compare(AddressListBean o1, AddressListBean o2) {if (o1.getSortLetters().equals("@") || o2.getSortLetters().equals("#")) {return -1;} else if (o1.getSortLetters().equals("#")|| o2.getSortLetters().equals("@")) {return 1;} else {return o1.getSortLetters().compareTo(o2.getSortLetters());}}}

最后在对需要的汉字进行后,需要对在Bean中保存改名字的首字母,为的是在下面做分类的时候用。

根据字母对列表分类

分类比较简单,主要判断首字母是否相同即可

 if (position != 0 && bean.getSortLetters().equals(data.get(position - 1).getSortLetters())) {viewHolder.tv_title.setVisibility(View.GONE);} else {viewHolder.tv_title.setText(bean.getSortLetters());viewHolder.tv_title.setVisibility(View.VISIBLE);}

但是这里需要对首字母第一次出现的时候做一次记录,因为SideBar每次滑动的时候,需要滚动的不是一条,而是一个类别,所有还需要有:

public int getPositionForSection(int section) {for (int i = 0; i < getCount(); i++) {String sortStr = data.get(i).getSortLetters();char firstChar = sortStr.toUpperCase().charAt(0);if (firstChar == section) {return i;}}return -1;}

这样呢,基本就实现 了通讯录的基本功能。

写滑动事件

如果需要SideBar和侧边栏做联动,则需要实现SideBar的滑动监听事件,具体写法如下:

 sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {@Overridepublic void onTouchingLetterChanged(String s) {//滑动侧边栏执行的操作int position = adapter.getPositionForSection(s.charAt(0));if (position != -1) {lv_listView.setSelection(position);}sideBar.setBackgroundColor(Color.parseColor("#FFDCDCDC")); //滑动时更改侧边栏颜色}});

如果需要修改SideBar的字体颜色,和选中的字体颜色,只需要修改SideBar中的Paint颜色即可。
到这里,SideBar显现的字母为A-Z,如果要实现微信那样,在列表中有多少人,右侧的导航栏就显示多少字母缩写,则还需要对SideBar的数据进行重新设置,我在SideBar中加了一个方法,设置如下:

 /*** 用于存放当前已有的名字标识集合,若默认显示A-Z,则不需要实现*/ArrayList<String> b = new ArrayList<String>();/*** 根据数据,动态生成侧边栏*/for (int i = 0; i < data.size(); i++) {String s = data.get(i).getSortLetters();if (i == 0) {b.add(s);} else {if (!s.equals(data.get(i - 1).getSortLetters())) {Log.i("yuanglang", s);b.add(s);}}}sideBar.setB(b); //重新设置侧边栏集合sideBar.invalidate(); //刷新侧边栏

这样就基本实现了,xml代码如下,界面比较丑,大家可自行美化。
main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="lovelder.lovcreate.com.sidebarapplication.MainActivity"><ListView
        android:id="@+id/lv_listView"android:layout_width="match_parent"android:layout_height="match_parent"></ListView><LinearLayout
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right|center"android:layout_marginBottom="50dp"android:layout_marginTop="50dp"><lovelder.lovcreate.com.sidebarapplication.SideBar
            android:id="@+id/sideBar"android:layout_width="25.0dip"android:layout_height="fill_parent"android:layout_marginBottom="10dp"android:layout_marginTop="10dp"></lovelder.lovcreate.com.sidebarapplication.SideBar></LinearLayout></FrameLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"><TextView
        android:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="A"android:paddingLeft="10dp"android:paddingTop="2dp"android:background="#f5f5f5"android:paddingBottom="2dp"/><LinearLayout
        android:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"android:gravity="center_vertical"android:background="#ffffff"android:paddingLeft="10dp"><ImageView
            android:id="@+id/iv_head"android:layout_width="35dp"android:layout_height="35dp"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher"/><LinearLayout
            android:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="vertical"android:layout_marginLeft="15dp"android:gravity="center_vertical"><TextView
                android:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:text="机电工程学院"/><TextView
                android:id="@+id/tv_phone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:visibility="visible" /></LinearLayout></LinearLayout><TextView
        android:layout_width="match_parent"android:layout_height="1px"android:background="#cccbcb"/></LinearLayout>

Demo下载

SideBar实现微信通讯录相关推荐

  1. 通过SectionIndexer实现微信通讯录

    这里主要参考了使用SectionIndexer实现微信通讯录的效果 在这里做个记录 效果图 页面使用RelativeLayout,主要分为三个部分,match_parent的主listView,右边字 ...

  2. Android仿微信通讯录

    Android仿微信通讯录 分3部: 1.listview实现显示头像.名字(太简单,这里就不写了) 通讯录页面xml布局代码: <LinearLayout xmlns:android=&quo ...

  3. android 仿微信demo————微信通讯录界面功能实现(移动端,服务端)

    android 仿微信demo----微信启动界面实现 android 仿微信demo----注册功能实现(移动端) android 仿微信demo----注册功能实现(服务端) android 仿微 ...

  4. 自定义实现微信通讯录效果View

    欢迎访问我的个人独立博客 ittiger.cn,原创文章,未经允许不得随意转载. 前言 在使用App过程中,经常会有使用到联系人或城市列表的场景,其实这两种效果是一样的,都是右边有个索引列表,点击索引 ...

  5. 【Android 仿微信通讯录 导航分组列表-上】使用ItemDecoration为RecyclerView打造带悬停头部的分组列表

    *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52355199 本文 ...

  6. RN仿微信通讯录列表

    源码在此 先看一下预览图效果: pic1.jpg 首先通过构造器初始化state constructor(props) {super(props);this.state = {//Global这里是全 ...

  7. 通讯录新建分组功能php,微信通讯录分组怎么设置

    1在通讯录里找到"标签"并点击2可通过图中两种按钮新建标签3选择要新建分组中的好友,选择完毕后点右上角的"确定"4可编辑分组名.添加和删除分组成员5完成后分组列 ...

  8. android 字母搜索栏,android仿微信通讯录搜索示例(匹配拼音,字母,索引位置)

    前言: 仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置 一:先看效果图 字母索引 搜索匹配 二:功能分析 1:汉字转拼音 通讯录汉字转拼音(首个字符当考虑姓氏多音字), 现 ...

  9. Android Studio开发(四)SQLite数据库的DAO标准CRUD操作模拟微信通讯录

    Android Studio开发(四)SQLite数据库的DAO标准CRUD操作模拟微信通讯录 Android Studio开发(四)SQLite数据库的DAO标准CRUD操作模拟微信通讯录 一.任务 ...

最新文章

  1. 为了成长,我所做的一些努力!
  2. 宝塔Linux, 反向代理服务器, 开启WSS
  3. mysql 启动参数_MySQL启动相关参数 MySQL DBA学习
  4. 以下属于单例模式的优点的是_三、单例模式详解
  5. ubuntu运行docker报错:invalid mount config for type “bind“: field Source must not be empty.(没解决)
  6. java try catch陷阱_Java异常处理最佳实践及陷阱防范
  7. 不会和容器一起启动_一起学习docker05-docker容器
  8. 解决thrift: ···No such file or directory问题
  9. 决策的五个要素 by 彼得。德鲁克
  10. 集群环境下的文件上传和下载和文件服务器的配置
  11. html 页面的分析与设计,HTML+CSS网页设计教程
  12. 谈到电影,我们收获了什么
  13. Channel is reciprocal
  14. mongodb数据库
  15. 五个学习管理系统的优点
  16. mysql数据库存储经度纬度
  17. 01背包、完全背包(异同)
  18. 【微观】需求、供给和均衡价格
  19. 深度学习数学基础之线性代数
  20. android小问题备忘

热门文章

  1. 企业邮箱注册申请入口,公司邮箱申请哪个好?
  2. 机器学习需要掌握的数学知识点---详细整理
  3. 微信小程序踩坑–卸载所有页面(含tabBar)跳转到指定页面
  4. 关于访问微信安全域名认证txt文件访问的通用解决方案
  5. 使用IText组件在PDF文档上绘制椭圆形印章的算法分析及代码分享
  6. 解决IE 自带的密码框 小眼睛与输入框X符号
  7. 武汉计算机软件应届毕业生工资,精打细算告诉你一个应届毕业生在武汉工资多少才能活下来(汉口物件)...
  8. CK-GW06-E01网关与欧姆龙PLC配置指南
  9. 【MySQL】数据库中的三大范式
  10. MavenLombok