首页 > 系统 > Android > 正文

Android实现仿通讯录侧边栏滑动SiderBar效果代码

2019-10-24 20:34:57
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android实现仿通讯录侧边栏滑动SiderBar效果代码,实例分析了通讯录侧边栏滑动效果的实现技巧,并附带完整实例代码供读者下载参考,需要的朋友可以参考下

本文实例讲述了Android实现仿通讯录侧边栏滑动SiderBar效果代码。分享给大家供大家参考,具体如下:

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

先上图:

Android实现仿通讯录侧边栏滑动SiderBar效果代码

具体实现的代码如下:

 

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

然后ListContantsUtil类是存储通讯录名字的拼音等的类,具体也如下:

 

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

注意,上面的程序还是有些小问题的,请大家注意优化解决。

完整实例代码代码点击此处本站下载。

希望本文所述对大家Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表