Android 仿通讯录侧边栏滑动 SiderBar效果

转载自:http://blog.csdn.net/freesonhp/article/details/9902939

之前看到某些应用的侧边栏做得不错,想想自己也弄一个出来,现在分享出来,当然里面还有不足的地方,请大家多多包涵。

先上图:


具体实现的代码如下:

  1. package com.freesonfish.listview_index;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10.   
  11. public class MySideBar extends View {  
  12.   
  13.     private OnTouchingLetterChangedListener touchListener;  
  14.     // 26个字母  
  15.     public static String[] b = { "#""A""B""C""D""E""F""G""H""I""J""K""L""M""N""O""P""Q""R""S""T",  
  16.             "U""V""W""X""Y""Z" };  
  17.   
  18.     private boolean showBkg = false;  
  19.     int choose = -1;  
  20.     int scrollChoose = -1;  
  21.     Paint paint = new Paint();  
  22.     Paint rectPaint = new Paint();  
  23.     float rectWidth = 0f;  
  24.   
  25.     public MySideBar(Context context, AttributeSet attrs, int defStyle) {  
  26.         super(context, attrs, defStyle);  
  27.         init();  
  28.     }  
  29.   
  30.     public MySideBar(Context context, AttributeSet attrs) {  
  31.         super(context, attrs);  
  32.         init();  
  33.     }  
  34.   
  35.     public MySideBar(Context context) {  
  36.         super(context);  
  37.         init();  
  38.     }  
  39.   
  40.     private void init() {  
  41.         rectPaint.setColor(Color.parseColor("#CCCCCC"));  
  42.         rectWidth = paint.measureText("#");  
  43.     }  
  44.   
  45.     /** 
  46.      * 重写这个方法 
  47.      */  
  48.     protected void onDraw(Canvas canvas) {  
  49.         super.onDraw(canvas);  
  50.         if (showBkg) {  
  51.             canvas.drawColor(Color.parseColor("#CCCCCC"));  
  52.         }  
  53.         final int height = getHeight();  
  54.         final int width = getWidth();  
  55.         final int singleHeight = height / b.length;  
  56.         final float xRectPos = ((float) width - paint.measureText("#")) / 2.0f - rectWidth;  
  57.         final float xRectPos2 = xRectPos + 3.0f * rectWidth;  
  58.         for (int i = 0; i < b.length; i++) {  
  59.             paint.setFakeBoldText(true);  
  60.             paint.setAntiAlias(true);  
  61.             final float xPos = ((float) width - paint.measureText(b[i])) / 2.0f;  
  62.             final float yPos = singleHeight * i + singleHeight;  
  63.             if (i == choose) {  
  64.                 paint.setColor(Color.RED);  
  65.                 canvas.drawRect(xRectPos, yPos - singleHeight / 2.0f, xRectPos2, yPos + rectWidth, rectPaint);  
  66.             }  
  67.             canvas.drawText(b[i], xPos, yPos, paint);  
  68.             paint.reset();  
  69.         }  
  70.   
  71.     }  
  72.   
  73.     @Override  
  74.     public boolean dispatchTouchEvent(MotionEvent event) {  
  75.         final int action = event.getAction();  
  76.         final float y = event.getY();  
  77.         final int c = (int) (y / getHeight() * b.length);  
  78.         switch (action) {  
  79.         case MotionEvent.ACTION_DOWN:  
  80.             showBkg = true;  
  81.             if (choose != c && touchListener != null) {  
  82.                 doOnActionDown(c);  
  83.             }  
  84.             break;  
  85.         case MotionEvent.ACTION_MOVE:  
  86.             if (choose != c && touchListener != null) {  
  87.                 doOnActionDown(c);  
  88.             }  
  89.             break;  
  90.         case MotionEvent.ACTION_UP:  
  91.             showBkg = false;  
  92.             invalidate();  
  93.             break;  
  94.         }  
  95.         return true;  
  96.     }  
  97.   
  98.     /** 
  99.      * listview滚动时候调用它 
  100.      *  
  101.      * @param c 
  102.      */  
  103.     public void setColorWhenListViewScrolling(int c) {  
  104.         if (scrollChoose != c) {  
  105.             scrollChoose = c;  
  106.             String string = ListContantsUtil.AbcList.get(c);  
  107.             for (int i = c; i < b.length; ++i) {  
  108.                 if (string.equals(b[i])) {  
  109.                     choose = i;  
  110.                     invalidate();  
  111.                     break;  
  112.                 }  
  113.             }  
  114.         }  
  115.     }  
  116.   
  117.     /** 
  118.      * 当侧边栏被按下的动作 
  119.      * @param c 
  120.      */  
  121.     private void doOnActionDown(int c) {  
  122.         if (c > 0 && c < b.length) {  
  123.             if (ListContantsUtil.indexPositionMap.containsKey(b[c])) {  
  124.                 touchListener.onTouchingLetterChanged(b[c]);  
  125.                 choose = c;  
  126.                 invalidate();  
  127.             } else {  
  128.                 c = c - 1;  
  129.                 doOnActionDown(c);  
  130.             }  
  131.         }  
  132.     }  
  133.   
  134.     @Override  
  135.     public boolean onTouchEvent(MotionEvent event) {  
  136.         return super.onTouchEvent(event);  
  137.     }  
  138.   
  139.     public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener touchListener) {  
  140.         this.touchListener = touchListener;  
  141.     }  
  142.   
  143.     /** 
  144.      * 用来通知activity显示选中的字母 
  145.      * @author freeson 
  146.      * 
  147.      */  
  148.     public interface OnTouchingLetterChangedListener {  
  149.         public void onTouchingLetterChanged(String s);  
  150.     }  
  151.   
  152. }  

然后ListContantsUtil类是存储通讯录名字的拼音等的类,具体也如下:
  1. package com.freesonfish.listview_index;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. public class ListContantsUtil {  
  8.   
  9.     static final List<Integer> indexPositionList = new ArrayList<Integer>();  
  10.     static final List<String> AbcList = new ArrayList<String>();  
  11.     static final HashMap<String, Integer> indexPositionMap = new HashMap<String, Integer>();  
  12.   
  13.     static void putNameIndexToMap(List<String> list, HashMap<String, String> nameAndPinyin) {  
  14.         int lenght = list.size();  
  15.         for (int i = 0; i < lenght; ++i) {  
  16.             String name = nameAndPinyin.get(list.get(i)).substring(01);  
  17.             // 判断该字符是属于字母还是数字或其他的  
  18.             int ascii = name.toCharArray()[0];  
  19.             if (ascii >= 65 && ascii <= 90) {  
  20.                 if (!indexPositionMap.containsKey(name)) {  
  21.                     indexPositionMap.put(name, i);  
  22.                     AbcList.add(name);  
  23.                     indexPositionList.add(i);  
  24.                 }  
  25.             } else {  
  26.                 if (!indexPositionMap.containsKey("#")) {  
  27.                     indexPositionMap.put("#", i);  
  28.                     AbcList.add("#");  
  29.                     indexPositionList.add(i);  
  30.                 }  
  31.             }  
  32.   
  33.         }  
  34.   
  35.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值