android之带右侧字母(拼音)索引的列表 - Go语言中文社区

android之带右侧字母(拼音)索引的列表


 在开发app的过程中,如果用到通讯录或者类似的列表,需要快速在其中定位,可以根据列表项的拼音首字母来定位,这时候就需要用到右侧字母索引了。必如现在的微信通讯录界面就是如此。在实现这种功能的过程中,还是挺复杂的,很难我觉得。在网上各种查找资料,困难重重,好在最后终于捯饬出来了,伤不起。。。。特此记录一下写的过程。

 

1、创建自定的view,用作右侧列表索引。

 

Java代码  收藏代码
  1. public class RulerWidget extends View {  
  2.       
  3.     public static String[] indexStr = {  
  4.         "#""A""B""C""D""E""F""G""H",  
  5.         "I""J""K""L""M""N""O""P""Q",  
  6.         "R""S""T""U""V""W""X""Y""Z"  
  7.         };  
  8.     public static int INDEX_LENGTH = indexStr.length;  
  9.       
  10.     OnTouchingLetterChangedListener onTouchingLetterChangedListener;   
  11.     Paint mPaint = new Paint();  
  12.     boolean showBkg = false;  
  13.     int choose = -1;  
  14.       
  15.     public RulerWidget(Context context) {  
  16.         super(context);  
  17.     }  
  18.       
  19.     public RulerWidget(Context context, AttributeSet attrs, int defStyle) {  
  20.         super(context, attrs, defStyle);  
  21.     }  
  22.       
  23.     public RulerWidget(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.     }  
  26.       
  27.     @Override  
  28.     protected void onDraw(Canvas canvas) {  
  29.         super.onDraw(canvas);  
  30. //      if(showBkg){    
  31.             canvas.drawColor(Color.parseColor("#40000000"));    
  32. //       }    
  33.                         
  34.         int height = getHeight();    
  35.         int width = getWidth();    
  36.         int singleHeight = height / indexStr.length;    
  37.         for(int i=0;i<indexStr.length;i++){    
  38.             mPaint.setColor(Color.WHITE);  
  39.             mPaint.setTextSize(24);  
  40.             mPaint.setTypeface(Typeface.DEFAULT_BOLD);    
  41.             mPaint.setAntiAlias(true);    
  42.             if(i == choose){    
  43.                 mPaint.setColor(Color.parseColor("#3399ff"));    
  44.                 mPaint.setFakeBoldText(true);    
  45.              }    
  46.             float xPos = width/2  - mPaint.measureText(indexStr[i])/2;    
  47.             float yPos = singleHeight * i + singleHeight;    
  48.             canvas.drawText(indexStr[i], xPos, yPos, mPaint);    
  49.             mPaint.reset();    
  50.         }    
  51.   
  52.     }  
  53.       
  54.       @Override    
  55.         public boolean dispatchTouchEvent(MotionEvent event) {    
  56.             final int action = event.getAction();    
  57.             final float y = event.getY();    
  58.             final int oldChoose = choose;    
  59.             final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;    
  60.             final int c = (int) (y/getHeight()*indexStr.length);    
  61.                 
  62.             switch (action) {    
  63.                 case MotionEvent.ACTION_DOWN:    
  64.                     showBkg = true;    
  65.                     if(oldChoose != c && listener != null){    
  66.                         if(c > 0 && c< indexStr.length){    
  67.                             listener.onTouchingLetterChanged(indexStr[c]);    
  68.                             choose = c;    
  69.                             invalidate();    
  70.                         }    
  71.                     }    
  72.                         
  73.                     break;    
  74.                 case MotionEvent.ACTION_MOVE:    
  75.                     if(oldChoose != c && listener != null){    
  76.                         if(c > 0 && c< indexStr.length){    
  77.                             listener.onTouchingLetterChanged(indexStr[c]);    
  78.                             choose = c;    
  79.                             invalidate();    
  80.                         }    
  81.                     }    
  82.                     break;    
  83.                 case MotionEvent.ACTION_UP:    
  84. //                  showBkg = false;    
  85.                     choose = -1;    
  86.                     invalidate();    
  87.                     break;    
  88.             }    
  89.             return true;    
  90.         }    
  91.         
  92.         @Override    
  93.         public boolean onTouchEvent(MotionEvent event) {    
  94.             return super.onTouchEvent(event);    
  95.         }    
  96.         
  97.         public void setOnTouchingLetterChangedListener(    
  98.                 OnTouchingLetterChangedListener onTouchingLetterChangedListener) {    
  99.             this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;    
  100.         }    
  101.         
  102. }  

 

对应的回调接口定义:

Java代码  收藏代码
  1. /**   
  2.  * @date 2014-9-3 
  3.  * @Description: ruler触摸回调 
  4.  */  
  5. public interface OnTouchingLetterChangedListener{    
  6.     public void onTouchingLetterChanged(String s);    
  7. }    

 

 

2、创建fragment片段对应的布局文件:

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <FrameLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:background="#ffffff" >  
  11.   
  12.         <ListView  
  13.             android:id="@+id/listView"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:cacheColorHint="#00000000"  
  17.             android:fadingEdge="none"  
  18.             android:scrollbars="none" >  
  19.   
  20.         </ListView>  
  21.           
  22.         <TextView  
  23.             android:id="@+id/tv"  
  24.             android:layout_width="60dp"  
  25.             android:layout_height="60dp"  
  26.             android:gravity="center"  
  27.             android:background="#f0606060"  
  28.             android:layout_gravity="center"  
  29.             android:text="A"  
  30.             android:textColor="#ffffff"  
  31.             android:textSize="30sp" />  
  32.   
  33.          <com.hy.ticket.view.RulerWidget  
  34.             android:id="@+id/sidrbar"  
  35.             android:layout_width="30.0dip"  
  36.             android:layout_height="fill_parent"  
  37.             android:layout_gravity="right|center"  />    
  38.     </FrameLayout>  
  39.   
  40. </LinearLayout>  

 

 

3、引入pinyin4j.jar包(版本建议使用最新),写一个工具类,包含获取中文字符串拼音首字母,获取中文拼音等方法。

 

Java代码  收藏代码
  1. public class StringHelper {  
  2.       
  3.     public static String getPingYin(String src) {  
  4.         char[] t = src.toCharArray();  
  5.         String[] strs = new String[t.length];  
  6.         HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();  
  7.         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  8.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  9.         format.setVCharType(HanyuPinyinVCharType.WITH_V);  
  10.         String str = "";  
  11.         try{  
  12.             for(int i=0; i<t.length; i++) {  
  13.                 if(java.lang.Character.toString(t[i]).matches(  
  14.                         "[\u4E00-\u9FA5]+")) {  
  15.                     strs = PinyinHelper.toHanyuPinyinStringArray(t[i], format);  
  16.                     str += strs[0];  
  17.                 }else{  
  18.                     str += java.lang.Character.toString(t[i]);  
  19.                 }  
  20.             }  
  21.              版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
    原文链接:https://blog.csdn.net/qq_31410081/article/details/78802485
    站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 10:28:15
  • 阅读 ( 1300 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢