这里主要参考了使用SectionIndexer实现微信通讯录的效果

在这里做个记录

效果图

页面使用RelativeLayout,主要分为三个部分,match_parent的主listView,右边字母的SideBar,还有就是微信那种点击字母时浮动的一个TextView

布局:

  fragment_contacts.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6     <ListView
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9         android:listSelector="@android:color/transparent"
10         android:id="@+id/listFragmentContacts"/>
11     <TextView
12         android:id="@+id/textFragmentContacts"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:layout_centerInParent="true"
16         android:padding="20dp"
17         android:textSize="40sp"
18         android:textColor="@android:color/white"
19         android:background="#33000000"
20         android:visibility="gone"
21         tools:visibility="visible"
22         tools:text="A"/>
23     <lee.example.com.testdj.customWeight.ContactsMySideBar
24         android:id="@+id/sideBarFragmentContacts"
25         android:layout_width="24dp"
26         android:layout_height="match_parent"
27         android:layout_alignParentRight="true"/>
28 </RelativeLayout>

  layout_contacts_item.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="wrap_content"
 6     android:orientation="vertical">
 7
 8     <!--section,表示组-->
 9     <TextView
10         android:id="@+id/tvSectionContactsItem"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:gravity="left"
14         android:textSize="22sp"
15         android:textColor="#666666"
16         tools:text="section"/>
17
18     <TextView
19         android:id="@+id/tvNormalContactsItem"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:padding="6dp"
23         android:textColor="@android:color/black"
24         android:background="@android:color/white"
25         android:textSize="20sp"
26         tools:text="text"/>
27 </LinearLayout>

实现:

首先,自定义View来实现SideBar,ContactsMySideBar.java

  1 public class ContactsMySideBar extends View{
  2
  3     private static final String TAG = "ContactsMySideBar";
  4
  5     private SectionIndexer sectionIndexer;
  6     private final char[] letters = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  7     private Paint paint;                    //画笔用来绘制字母
  8     private ListView listView;
  9     private int focusedIndex = -1;          //点击选中的索引
 10     private int drawWidth, drawHeight;      //绘制单个字母的宽高
 11
 12     public interface OnTouchChangedListener{
 13         void onTouchDown(char c);
 14         void onTouchUp();
 15     }
 16
 17     private OnTouchChangedListener listener;
 18
 19     public void setOnTouchChangedListener(OnTouchChangedListener listener){
 20         this.listener = listener;
 21     }
 22
 23     public void setListView(ListView listView){
 24         this.listView = listView;
 25         sectionIndexer = (SectionIndexer) listView.getAdapter();
 26     }
 27
 28     public ContactsMySideBar(Context context) {
 29         super(context);
 30         init();
 31     }
 32
 33     public ContactsMySideBar(Context context, AttributeSet attrs) {
 34         super(context, attrs);
 35         init();
 36     }
 37
 38     public ContactsMySideBar(Context context, AttributeSet attrs, int defStyle) {
 39         super(context, attrs, defStyle);
 40         init();
 41     }
 42
 43     private void init(){
 44         //设置画笔属性
 45         paint = new Paint();
 46         paint.setColor(Color.GRAY);
 47         paint.setTextSize(18f);
 48         paint.setTextAlign(Paint.Align.CENTER);
 49     }
 50
 51     @Override
 52     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 53         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 54         drawWidth = getMeasuredWidth()/2;           //宽度为sideBar的一半
 55         drawHeight = getMeasuredHeight()/letters.length;    //设置高度
 56     }
 57
 58     @Override
 59     protected void onDraw(Canvas canvas) {
 60         for (int i = 0; i < letters.length; i++){
 61             canvas.drawText(String.valueOf(letters[i]), drawWidth, drawHeight + (i * drawHeight), paint);
 62         }
 63     }
 64
 65     @Override
 66     public boolean onTouchEvent(MotionEvent event) {
 67         int pointerY = (int) event.getY();
 68         int selectedIndex = pointerY / drawHeight;
 69         if (selectedIndex >= letters.length){
 70             selectedIndex = letters.length - 1;
 71         }else if(selectedIndex < 0){
 72             selectedIndex = 0;
 73         }
 74
 75         switch (event.getAction()){
 76             case MotionEvent.ACTION_DOWN:
 77                 //点击时设置为半透明
 78                 setBackgroundColor(Color.parseColor("#33000000"));
 79             case MotionEvent.ACTION_MOVE:
 80                 if (sectionIndexer == null){
 81                     sectionIndexer = (SectionIndexer) listView.getAdapter();
 82                 }
 83 //                Log.d(TAG, letters[selectedIndex] + "");
 84                 //根据数组中的元素获取对应的组位置
 85                 int position = sectionIndexer.getPositionForSection(letters[selectedIndex]);
 86                 Log.d(TAG, "" + position);
 87                 if (position == -1){
 88                     return true;
 89                 }
 90                 if (selectedIndex != 0){
 91                     if (position != 0){
 92                         //改变当前listView所处位置
 93                         listView.setSelection(position);
 94                     }
 95                 }else {
 96                     listView.setSelection(position);
 97                 }
 98
 99                 //重绘SideBar
100                 invalidate();
101                 if (null != listener){
102                     listener.onTouchDown(letters[selectedIndex]);
103                 }
104                 break;
105             case MotionEvent.ACTION_UP:
106             case MotionEvent.ACTION_CANCEL:
107                 //松开手指取消背景色
108                 setBackgroundResource(android.R.color.transparent);
109                 invalidate();
110                 if (null != listener){
111                     listener.onTouchUp();
112                 }
113                 break;
114         }
115         return true;
116     }
117 }

这部分代码很简单,重写了OnMeasure和OnDraw来绘制SideBar,在OnTouchEvent中写出了SideBar的响应事件

定义listView的adapter

SideBarAdapter.java
  1 public class SideBarAdapter extends BaseAdapter implements SectionIndexer{
  2
  3     private static final String TAG = "SideBarAdapter";
  4
  5     private Context context;
  6     private List<ContactsModel> modelList;
  7     private LayoutInflater layoutInflater;
  8
  9     public SideBarAdapter(Context context, List<ContactsModel> modelList){
 10         this.context = context;
 11         this.modelList = modelList;
 12         layoutInflater = LayoutInflater.from(context);
 13     }
 14
 15     @Override
 16     public int getCount() {
 17         return modelList.size();
 18     }
 19
 20     @Override
 21     public ContactsModel getItem(int i) {
 22         return modelList.get(i);
 23     }
 24
 25     @Override
 26     public long getItemId(int i) {
 27         return i;
 28     }
 29
 30     @Override
 31     public View getView(int i, View convertView, ViewGroup viewGroup) {
 32         Holder holder;
 33         if (convertView == null){
 34             convertView = layoutInflater.inflate(R.layout.layout_contacts_item, viewGroup, false);
 35             holder = new Holder();
 36             holder.normalTv = (TextView) convertView.findViewById(R.id.tvNormalContactsItem);
 37             holder.sectionTv = (TextView) convertView.findViewById(R.id.tvSectionContactsItem);
 38             convertView.setTag(holder);
 39         }else {
 40             holder = (Holder) convertView.getTag();
 41         }
 42         String text = modelList.get(i).getName();
 43         setSectionTv(i, holder, text);
 44         setNormalTv(holder, text);
 45
 46         return convertView;
 47     }
 48
 49     private void setNormalTv(Holder holder, String text){
 50         holder.normalTv.setText(text);
 51     }
 52
 53     private void setSectionTv(int position, Holder holder, String text){
 54         //获取每个item字符串的头一个字符
 55 //        Log.d(TAG, ZhCharactersUtil.changeToSpell(text));
 56         char firstChar = ZhCharactersUtil.changeToSpell(text).toUpperCase().charAt(0);
 57         //若为第一个位置直接设置组view就行
 58         if (position == 0) {
 59             holder.sectionTv.setVisibility(View.VISIBLE);
 60             holder.sectionTv.setText((firstChar + "").toUpperCase());
 61         }
 62         //若不是,需判断当前item首字母与上一个item首字母是否一致,再设置组view
 63         else {
 64             String preLabel = modelList.get(position - 1).getName();
 65             //获取上一个item的首字母
 66             char preFirstChar = ZhCharactersUtil.changeToSpell(preLabel).toUpperCase().charAt(0);
 67             if (firstChar != preFirstChar) {
 68                 holder.sectionTv.setVisibility(View.VISIBLE);
 69                 holder.sectionTv.setText((firstChar + "").toUpperCase());
 70             } else {
 71                 //若与上一个item首字母一致则不需要重复设置组view
 72                 holder.sectionTv.setVisibility(View.GONE);
 73             }
 74         }
 75     }
 76
 77     @Override
 78     public Object[] getSections() {
 79         //获取组信息的数组,比如这里可以返回char[]{'A','B',...}
 80         return new Object[0];
 81     }
 82
 83     @Override
 84     public int getPositionForSection(int section) {
 85         //根据组信息获取索引
 86         for (int i = 0; i < modelList.size(); i++) {
 87             String str = modelList.get(i).getName();
 88             char firstChar = ZhCharactersUtil.changeToSpell(str).toUpperCase().charAt(0);
 89             if (firstChar == section) {
 90                 return i;
 91             }
 92         }
 93         return 0;
 94     }
 95
 96     @Override
 97     public int getSectionForPosition(int i) {
 98         //根据索引获取组信息,这里不做处理
 99         return 0;
100     }
101
102     private final class Holder {
103         TextView normalTv, sectionTv;
104     }
105 }

这里用到了一个第三方,jpinyin,是用来对汉字进行处理,转换成拼音等等,ZhCharactersUtil.java就是用来处理汉字的,我这里只需要取拼音的第一个字母

1 public class ZhCharactersUtil {
2     public static String changeToSpell(String string){
3         char[] chars = PinyinHelper.getShortPinyin(string).toCharArray();
4         return chars[0] + "";
5     }
6
7 }

这里的ContactsModel里边只有两个属性,一个name,一个firstCharacter,其中firstCharacter是用来进行排序的,上边adapter的代码可以用modelList.get(i).getFirstCharacter()来简化一些,偷懒了,所以开始写好了就放在那没动,这里这个model就不记录了。

在Activity中使用,我这里是在fragment中使用,都是一样的

1 private ListView listView;
2     //悬浮的textView
3     private TextView maskText;
4     private ContactsMySideBar sideBar;
5
6     //然后对他们进行初始化

 1 private void initData(){
 2         ContactsModel model1 = new ContactsModel("张一");
 3         ContactsModel model2 = new ContactsModel("张二");
 4         ContactsModel model3 = new ContactsModel("李一");
 5         ContactsModel model4 = new ContactsModel("李二");
 6         ContactsModel model5 = new ContactsModel("赵一");
 7         ContactsModel model6 = new ContactsModel("赵二");
 8         ContactsModel model7 = new ContactsModel("王三");
 9         ContactsModel model8 = new ContactsModel("王二");
10         ContactsModel model9 = new ContactsModel("阿张");
11         ContactsModel model10 = new ContactsModel("不行");
12         ContactsModel model11 = new ContactsModel("特别");
13         ContactsModel model12 = new ContactsModel("将就");
14         ContactsModel model13 = new ContactsModel("公司");
15         ContactsModel model14 = new ContactsModel("空是");
16         ContactsModel model15 = new ContactsModel("好附件打开");
17         ContactsModel model16 = new ContactsModel("现在");
18         ContactsModel model17 = new ContactsModel("哦跑");
19
20         ContactsModel model18 = new ContactsModel("aaasss");
21         ContactsModel model19 = new ContactsModel("bbb");
22         ContactsModel model20 = new ContactsModel("s");
23         ContactsModel model21 = new ContactsModel("jjj");
24         ContactsModel model22 = new ContactsModel("oot");
25
26         modelList.add(model1);
27         modelList.add(model2);
28         modelList.add(model3);
29         modelList.add(model4);
30         modelList.add(model5);
31         modelList.add(model6);
32         modelList.add(model7);
33         modelList.add(model8);
34         modelList.add(model9);
35         modelList.add(model10);
36         modelList.add(model11);
37         modelList.add(model12);
38         modelList.add(model13);
39         modelList.add(model14);
40         modelList.add(model15);
41         modelList.add(model16);
42         modelList.add(model17);
43 //        modelList.add(model18);
44 //        modelList.add(model19);
45 //        modelList.add(model20);
46 //        modelList.add(model21);
47 //        modelList.add(model22);
48         Collections.sort(modelList, new Comparator<ContactsModel>() {
49             @Override
50             public int compare(ContactsModel contactsModel, ContactsModel t1) {
51                 return contactsModel.getFirstCharacter().compareTo(t1.getFirstCharacter());
52             }
53         });
54
55         adapter = new SideBarAdapter(getContext(), modelList);
56         listView.setAdapter(adapter);
57         sideBar.setListView(listView);
58         sideBar.setOnTouchChangedListener(new ContactsMySideBar.OnTouchChangedListener() {
59             @Override
60             public void onTouchDown(char c) {
61                 maskText.setText(c + "");
62                 maskText.setVisibility(View.VISIBLE);
63             }
64
65             @Override
66             public void onTouchUp() {
67                 maskText.setVisibility(View.GONE);
68             }
69         });
70     }

这里添加了一些假数据,完成。

转载于:https://www.cnblogs.com/devli/p/5340935.html

通过SectionIndexer实现微信通讯录相关推荐

  1. Android仿微信通讯录

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

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

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

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

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

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

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

  5. 怎么恢复删除的微信通讯录好友?这样恢复将友谊进行到底!

    怎么恢复删除的微信通讯录好友?微信是我们非常重要的沟通软件,现如今,两个好友之间从陌生到熟悉再到陌生,微信起着举足轻重的作用.很多人一开始相谈甚欢,关系特别好,但随着时间的流逝,渐渐少了联系,如若忘了 ...

  6. Android微信通讯录界面代码,Android中使用Expandablelistview实现微信通讯录界面

    之前的博文<Android 中使用ExpandableListView 实现分组的实例>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的 ...

  7. 【uniapp前端组件】仿微信通讯录列表组件

    仿微信通讯录列表组件 示例图 前言 仿微信通讯录列表组件,可实现通讯列表以及选择多个联系人功能. 组件介绍 本组件有三个自定义组件构成,都已经集成在bugking7-contact-list中,该组件 ...

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

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

  9. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

最新文章

  1. 静态链接中的那点事儿(1)
  2. Ubuntu 及其衍生版安装使用截图工具【深度截图】
  3. 看看C# 6.0中那些语法糖都干了些什么(上篇)
  4. 《零基础看得懂的C语言入门教程 》——(二)C语言没那么难简单开发带你了解流程
  5. linux实验三makefile,实验平台上Makefile详细的解释
  6. 解析poj页面获取题目
  7. ShadeGraph教程之节点详解3:Input Nodes
  8. xml TO json
  9. Python: 如何将py文件转成exe文件?
  10. .sql文件如何执行_一条SQL查询语句是如何执行的?
  11. 1.Zabbix企业级分布式监控系统 --- 监控系统简介
  12. SSM通信研究:如何拦截SSM代理流量
  13. vue项目 报sockjs.js?9be2:1606 GET http://192.168.43.226:8080/sockjs-node/info?t=1584966826465 net::ERR
  14. Scade Suite开发 ARINC 661 (2)Scade Suite基本操作
  15. 微信公众号前端40163解决办法
  16. 集群容错机制:failover、failfast、failback、failsafe、forking
  17. python繁简体转换
  18. PC版微信,公众号文章图片无法加载,解决方法
  19. 电脑可以连接手机热点,却无法连路由器无线wifi
  20. 基于微信小程序的小程序记账本APP源码

热门文章

  1. SiteEngine 6.071. SQLInjection
  2. Ubuntu 17.04 壁纸设计大赛 已经开幕
  3. C++再议构造函数及复制构造函数深度复制
  4. 错误提示:ssh: Could not resolve hostname devsrv: Name or service not known
  5. 【转】完整java开发中JDBC连接数据库代码和步骤
  6. RAID及LVM,iscsi
  7. vco为什么低频下起振困难_为什么开放如此困难?
  8. 怎么成为开源贡献者_开源如何成为您下一份工作的门户
  9. (15)Vue.js 计算属性
  10. html显示docx,网页中显示PDF的HTML代码.docx