Android仿微信(二)——仿微信联系人 首字母分类

效果图

代码:http://download.csdn.net/detail/b275518834/5753931

实现这个效果,需要三个知识点 :

1:将字符串 进行拼音分类

2:ExpandableListView 二级扩展列表

3:右边字母分类View

4:中间的绿色对话框popupWindow


首先字母分类代码  获得字符串首字母 首字符 

[java] view plain copy
  1. //获得字符串的首字母 首字符 转汉语拼音  
  2.     public  String getFirstChar(String value) {  
  3.         // 首字符  
  4.         char firstChar = value.charAt(0);  
  5.         // 首字母分类  
  6.         String first = null;  
  7.         // 是否是非汉字  
  8.         String[] print = PinyinHelper.toHanyuPinyinStringArray(firstChar);  
  9.   
  10.         if (print == null) {  
  11.   
  12.             // 将小写字母改成大写  
  13.             if ((firstChar >= 97 && firstChar <= 122)) {  
  14.                 firstChar -= 32;  
  15.             }  
  16.             if (firstChar >= 65 && firstChar <= 90) {  
  17.                 first = String.valueOf((char) firstChar);  
  18.             } else {  
  19.                 // 认为首字符为数字或者特殊字符  
  20.                 first = "#";  
  21.             }  
  22.         } else {  
  23.             // 如果是中文 分类大写字母  
  24.             first = String.valueOf((char)(print[0].charAt(0) -32));  
  25.         }  
  26.         if (first == null) {  
  27.             first = "?";  
  28.         }  
  29.         return first;  
  30.     }  

汉字排序

[java] view plain copy
  1. /** 
  2.  * 汉字排序 
  3.  * */  
  4. public class LanguageComparator_CN implements Comparator<String> {  
  5.       
  6.   
  7.     public int compare(String ostr1, String ostr2) {  
  8.   
  9.         for (int i = 0; i < ostr1.length() && i < ostr2.length(); i++) {  
  10.   
  11.             int codePoint1 = ostr1.charAt(i);  
  12.             int codePoint2 = ostr2.charAt(i);  
  13.             if (Character.isSupplementaryCodePoint(codePoint1)  
  14.                     || Character.isSupplementaryCodePoint(codePoint2)) {  
  15.                 i++;  
  16.             }  
  17.             if (codePoint1 != codePoint2) {  
  18.                 if (Character.isSupplementaryCodePoint(codePoint1)  
  19.                         || Character.isSupplementaryCodePoint(codePoint2)) {  
  20.                     return codePoint1 - codePoint2;  
  21.                 }  
  22.                 String pinyin1 = pinyin((char) codePoint1);  
  23.                 String pinyin2 = pinyin((char) codePoint2);  
  24.   
  25.                 if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字  
  26.                     if (!pinyin1.equals(pinyin2)) {  
  27.                         return pinyin1.compareTo(pinyin2);  
  28.                     }  
  29.                 } else {  
  30.                     return codePoint1 - codePoint2;  
  31.                 }  
  32.             }  
  33.         }  
  34.         return ostr1.length() - ostr2.length();  
  35.     }  
  36.   
  37.     // 获得汉字拼音的首字符  
  38.     private String pinyin(char c) {  
  39.         String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);  
  40.         if (pinyins == null) {  
  41.             return null;  
  42.         }  
  43.         return pinyins[0];  
  44.     }  
  45.   
  46. }  



2级列表展开

[java] view plain copy
  1.     //展开所有  
  2. or (int i = 0, length = adapter.getGroupCount(); i < length; i++) {  
  3. eListView.expandGroup(i);  

2级列表adapter

[java] view plain copy
  1. package com.cn.demo.pinyin;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.List;  
  6.   
  7. import org.pingyin.LanguageComparator_CN;  
  8.   
  9. import net.sourceforge.pinyin4j.PinyinHelper;  
  10.   
  11. import android.content.Context;  
  12. import android.view.Gravity;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.ViewGroup;  
  16. import android.widget.AbsListView.LayoutParams;  
  17. import android.widget.BaseExpandableListAdapter;  
  18. import android.widget.TextView;  
  19. import android.widget.Toast;  
  20.   
  21. public class PinyinAdapter extends BaseExpandableListAdapter {  
  22.   
  23.     // 字符串  
  24.     private List<String> strList;  
  25.   
  26.     private AssortPinyinList assort = new AssortPinyinList();  
  27.   
  28.     private Context context;  
  29.   
  30.     private LayoutInflater inflater;  
  31.     // 中文排序  
  32.     private LanguageComparator_CN cnSort = new LanguageComparator_CN();  
  33.   
  34.     public PinyinAdapter(Context context, List<String> strList) {  
  35.         super();  
  36.         this.context = context;  
  37.         this.inflater = LayoutInflater.from(context);  
  38.         this.strList = strList;  
  39.         if (strList == null) {  
  40.             strList = new ArrayList<String>();  
  41.         }  
  42.   
  43.         long time = System.currentTimeMillis();  
  44.         // 排序  
  45.         sort();  
  46.         Toast.makeText(context,  
  47.                 String.valueOf(System.currentTimeMillis() - time), 1).show();  
  48.   
  49.     }  
  50.   
  51.     private void sort() {  
  52.         // 分类  
  53.         for (String str : strList) {  
  54.             assort.getHashList().add(str);  
  55.         }  
  56.         assort.getHashList().sortKeyComparator(cnSort);  
  57.         for(int i=0,length=assort.getHashList().size();i<length;i++)  
  58.         {  
  59.             Collections.sort((assort.getHashList().getValueListIndex(i)),cnSort);  
  60.         }  
  61.           
  62.     }  
  63.   
  64.     public Object getChild(int group, int child) {  
  65.         // TODO Auto-generated method stub  
  66.         return assort.getHashList().getValueIndex(group, child);  
  67.     }  
  68.   
  69.     public long getChildId(int group, int child) {  
  70.         // TODO Auto-generated method stub  
  71.         return child;  
  72.     }  
  73.   
  74.     public View getChildView(int group, int child, boolean arg2,  
  75.             View contentView, ViewGroup arg4) {  
  76.         // TODO Auto-generated method stub  
  77.         if (contentView == null) {  
  78.             contentView = inflater.inflate(R.layout.adapter_chat, null);  
  79.         }  
  80.         TextView textView = (TextView) contentView.findViewById(R.id.name);  
  81.         textView.setText(assort.getHashList().getValueIndex(group, child));  
  82.         return contentView;  
  83.     }  
  84.   
  85.     public int getChildrenCount(int group) {  
  86.         // TODO Auto-generated method stub  
  87.         return assort.getHashList().getValueListIndex(group).size();  
  88.     }  
  89.   
  90.     public Object getGroup(int group) {  
  91.         // TODO Auto-generated method stub  
  92.         return assort.getHashList().getValueListIndex(group);  
  93.     }  
  94.   
  95.     public int getGroupCount() {  
  96.         // TODO Auto-generated method stub  
  97.         return assort.getHashList().size();  
  98.     }  
  99.   
  100.     public long getGroupId(int group) {  
  101.         // TODO Auto-generated method stub  
  102.         return group;  
  103.     }  
  104.   
  105.     public View getGroupView(int group, boolean arg1, View contentView,  
  106.             ViewGroup arg3) {  
  107.         if (contentView == null) {  
  108.             contentView = inflater.inflate(R.layout.list_group_item, null);  
  109.             contentView.setClickable(true);  
  110.         }  
  111.         TextView textView = (TextView) contentView.findViewById(R.id.name);  
  112.         textView.setText(assort.getFirstChar(assort.getHashList()  
  113.                 .getValueIndex(group, 0)));  
  114.         // 禁止伸展  
  115.   
  116.         return contentView;  
  117.     }  
  118.   
  119.     public boolean hasStableIds() {  
  120.         // TODO Auto-generated method stub  
  121.         return true;  
  122.     }  
  123.   
  124.     public boolean isChildSelectable(int arg0, int arg1) {  
  125.         // TODO Auto-generated method stub  
  126.         return true;  
  127.     }  
  128.   
  129.     public AssortPinyinList getAssort() {  
  130.         return assort;  
  131.     }  
  132.   
  133. }  


右边字母View编写


[java] view plain copy
  1. package com.cn.demo.pinyin;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Typeface;  
  8. import android.util.AttributeSet;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12.   
  13. public class AssortView extends Button {  
  14.   
  15.     public interface OnTouchAssortListener {  
  16.         public void onTouchAssortListener(String s);  
  17.         public void onTouchAssortUP();  
  18.     }  
  19.   
  20.     public AssortView(Context context) {  
  21.         super(context);  
  22.         // TODO Auto-generated constructor stub  
  23.     }  
  24.   
  25.     public AssortView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.         // TODO Auto-generated constructor stub  
  28.     }  
  29.   
  30.     public AssortView(Context context, AttributeSet attrs, int defStyle) {  
  31.         super(context, attrs, defStyle);  
  32.         // TODO Auto-generated constructor stub  
  33.     }  
  34.   
  35.     // 分类  
  36.     private String[] assort = { "?""#""A""B""C""D""E""F""G",  
  37.             "H""I""J""K""L""M""N""O""P""Q""R""S""T",  
  38.             "U""V""W""X""Y""Z" };  
  39.     private Paint paint = new Paint();  
  40.     // 选择的索引  
  41.     private int selectIndex = -1;  
  42.     // 字母监听器  
  43.     private OnTouchAssortListener onTouch;  
  44.   
  45.   
  46.     public void setOnTouchAssortListener(OnTouchAssortListener onTouch) {  
  47.         this.onTouch = onTouch;  
  48.     }  
  49.   
  50.     @Override  
  51.     protected void onDraw(Canvas canvas) {  
  52.         // TODO Auto-generated method stub  
  53.         super.onDraw(canvas);  
  54.         int height = getHeight();  
  55.         int width = getWidth();  
  56.         int interval = height / assort.length;  
  57.   
  58.         for (int i = 0, length = assort.length; i < length; i++) {  
  59.             // 抗锯齿  
  60.             paint.setAntiAlias(true);  
  61.             // 默认粗体  
  62.             paint.setTypeface(Typeface.DEFAULT_BOLD);  
  63.             // 白色  
  64.             paint.setColor(Color.WHITE);  
  65.             if (i == selectIndex) {  
  66.                 // 被选择的字母改变颜色和粗体  
  67.                 paint.setColor(Color.parseColor("#3399ff"));  
  68.                 paint.setFakeBoldText(true);  
  69.                 paint.setTextSize(30);  
  70.             }  
  71.             // 计算字母的X坐标  
  72.             float xPos = width / 2 - paint.measureText(assort[i]) / 2;  
  73.             // 计算字母的Y坐标  
  74.             float yPos = interval * i + interval;  
  75.             canvas.drawText(assort[i], xPos, yPos, paint);  
  76.             paint.reset();  
  77.         }  
  78.   
  79.     }  
  80.   
  81.     @Override  
  82.     public boolean dispatchTouchEvent(MotionEvent event) {  
  83.         // TODO Auto-generated method stub  
  84.         float y = event.getY();  
  85.         int index = (int) (y / getHeight() * assort.length);  
  86.         if (index >= 0 && index < assort.length) {  
  87.   
  88.             switch (event.getAction()) {  
  89.             case MotionEvent.ACTION_MOVE:  
  90.                 // 如果滑动改变  
  91.                 if (selectIndex != index) {  
  92.                     selectIndex = index;  
  93.                     if (onTouch != null) {  
  94.                         onTouch.onTouchAssortListener(assort[selectIndex]);  
  95.                     }  
  96.   
  97.                 }  
  98.                 break;  
  99.             case MotionEvent.ACTION_DOWN:  
  100.                 selectIndex = index;  
  101.                 if (onTouch != null) {  
  102.                     onTouch.onTouchAssortListener(assort[selectIndex]);  
  103.                 }  
  104.   
  105.                 break;  
  106.             case MotionEvent.ACTION_UP:  
  107.                 if (onTouch != null) {  
  108.                     onTouch.onTouchAssortUP();  
  109.                 }  
  110.                 selectIndex = -1;  
  111.                 break;  
  112.             }  
  113.         } else {  
  114.             selectIndex = -1;  
  115.             if (onTouch != null) {  
  116.                 onTouch.onTouchAssortUP();  
  117.             }  
  118.         }  
  119.         invalidate();  
  120.   
  121.         return true;  
  122.     }  
  123.   
  124.     @Override  
  125.     public boolean onTouchEvent(MotionEvent event) {  
  126.         return super.onTouchEvent(event);  
  127.     }  
  128. }  


中间绿色的对话框


[java] view plain copy
  1. //字母按键回调  
  2.         assortView.setOnTouchAssortListener(new OnTouchAssortListener() {  
  3.               
  4.             View layoutView=LayoutInflater.from(MainActivity.this)  
  5.                     .inflate(R.layout.alert_dialog_menu_layout, null);  
  6.             TextView text =(TextView) layoutView.findViewById(R.id.content);  
  7.             PopupWindow popupWindow ;  
  8.               
  9.             public void onTouchAssortListener(String str) {  
  10.                int index=adapter.getAssort().getHashList().indexOfKey(str);  
  11.                if(index!=-1)  
  12.                {  
  13.                     eListView.setSelectedGroup(index);;  
  14.                }  
  15.                 if(popupWindow!=null){  
  16.                 text.setText(str);  
  17.                 }  
  18.                 else  
  19.                 {     
  20.                       popupWindow = new PopupWindow(layoutView,  
  21.                             8080,  
  22.                             false);  
  23.                     // 显示在Activity的根视图中心  
  24.                     popupWindow.showAtLocation(getWindow().getDecorView(),  
  25.                             Gravity.CENTER, 00);  
  26.                 }  
  27.                 text.setText(str);  
  28.             }  
  29.   
  30.             public void onTouchAssortUP() {  
  31.                 if(popupWindow!=null)  
  32.                 popupWindow.dismiss();  
  33.                 popupWindow=null;  
  34.             }  
  35.         });  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值