[关闭]
@xujun94 2016-07-01T16:00:57.000000Z 字数 7880 阅读 986

常用的自定义控件四(QuickBarView)

自定义View 通讯录字母快速索引


在Android日常开发中,我们经常在联系人界面看到一些字母导航栏,点击字母的时候,会根据汉字的首拼音来查找是否存在相应的item,这种效果很常见,几乎所有涉及到通讯的都会用到,包括qq,微信,微博等,今天我为大家带来的就是这种自定义控件

废话不多说 ,大家先来看一下实际的效果

转载请注明原博客地址:http://blog.csdn.net/gdutxiaoxu/article/details/51804865
源码下载地址:https://github.com/gdutxiaoxu/QuickIndex.git

本文固定链接:https://www.zybuluo.com/xujun94/note/424224

大家先来看一下源码

  1. /**
  2. * 博客地址:http://blog.csdn.net/gdutxiaoxu
  3. * 快速索引,根据字母的索引查找相应的联系人
  4. *
  5. * @author xujun
  6. * @time 2015/11/1 21:40.
  7. */
  8. public class QuickIndexBar extends View {
  9. private static final String[] LETTERS = new String[]{
  10. "A", "B", "C", "D", "E", "F",
  11. "G", "H", "I", "J", "K", "L",
  12. "M", "N", "O", "P", "Q", "R",
  13. "S", "T", "U", "V", "W", "X",
  14. "Y", "Z"};
  15. private static final String TAG = "xujun";
  16. private Paint mPaint;
  17. //字母的宽度
  18. private int cellWidth;
  19. //字母的高度
  20. private float cellHeight;
  21. //记录上一次触摸的Index
  22. private int mLastTouchIndex = -1;
  23. //字母被选中显示的颜色
  24. private int mSelectColor = Color.GRAY;
  25. //字母正常显示的颜色
  26. private int mNormalColor = Color.WHITE;
  27. private Context mContext;
  28. /**
  29. * 暴露一个字母的监听
  30. */
  31. public interface OnLetterUpdateListener {
  32. void onLetterUpdate(String letter);
  33. }
  34. private OnLetterUpdateListener listener;
  35. public OnLetterUpdateListener getListener() {
  36. return listener;
  37. }
  38. /**
  39. * 设置字母更新监听
  40. *
  41. * @param listener
  42. */
  43. public void setListener(OnLetterUpdateListener listener) {
  44. this.listener = listener;
  45. }
  46. public QuickIndexBar(Context context) {
  47. this(context, null);
  48. }
  49. public QuickIndexBar(Context context, AttributeSet attrs) {
  50. this(context, attrs, 0);
  51. }
  52. public QuickIndexBar(Context context, AttributeSet attrs, int defStyle) {
  53. super(context, attrs, defStyle);
  54. mContext = context;
  55. //初始化自定义属性
  56. obtainAttrs(attrs);
  57. // 初始化画笔
  58. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  59. float textSize = UIUtils.dip2px(15, mContext);
  60. mPaint.setTextSize(textSize);
  61. mPaint.setTypeface(Typeface.DEFAULT_BOLD);
  62. }
  63. private void obtainAttrs(AttributeSet attrs) {
  64. TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.QuickIndexBar);
  65. int selectColor = typedArray.getColor(R.styleable.QuickIndexBar_select_color, -1);
  66. if (selectColor != -1) {
  67. mSelectColor = selectColor;
  68. }
  69. int normalColor = typedArray.getColor(R.styleable.QuickIndexBar_normal_color, -1);
  70. if (normalColor != -1) {
  71. mNormalColor = normalColor;
  72. }
  73. typedArray.recycle();
  74. }
  75. @Override
  76. protected void onDraw(Canvas canvas) {
  77. for (int i = 0; i < LETTERS.length; i++) {
  78. String text = LETTERS[i];
  79. // 计算坐标
  80. int x = (int) (cellWidth / 2.0f - mPaint.measureText(text) / 2.0f);
  81. // 获取文本的高度
  82. Rect bounds = new Rect();// 矩形
  83. mPaint.getTextBounds(text, 0, text.length(), bounds);
  84. int textHeight = bounds.height();
  85. int y = (int) (cellHeight / 2.0f + textHeight / 2.0f + i * cellHeight);
  86. // 根据按下的字母, 设置画笔颜色
  87. mPaint.setColor(mLastTouchIndex == i ? mSelectColor : mNormalColor);
  88. // 绘制文本A-Z
  89. canvas.drawText(text, x, y, mPaint);
  90. }
  91. }
  92. @Override
  93. public boolean onTouchEvent(MotionEvent event) {
  94. int index = -1;
  95. switch (MotionEventCompat.getActionMasked(event)) {
  96. case MotionEvent.ACTION_DOWN:
  97. // 获取当前触摸到的字母索引
  98. index = (int) (event.getY() / cellHeight);
  99. if (index >= 0 && index < LETTERS.length) {
  100. // 判断是否跟上一次触摸到的一样,不一样才进行回调
  101. if (index != mLastTouchIndex) {
  102. if (listener != null) {
  103. //
  104. listener.onLetterUpdate(LETTERS[index]);
  105. }
  106. Log.d(TAG, "onTouchEvent: " + LETTERS[index]);
  107. //记录上一次触摸的Index为当前的index;
  108. mLastTouchIndex = index;
  109. }
  110. }
  111. break;
  112. case MotionEvent.ACTION_MOVE:
  113. index = (int) (event.getY() / cellHeight);
  114. if (index >= 0 && index < LETTERS.length) {
  115. // 判断是否跟上一次触摸到的一样
  116. if (index != mLastTouchIndex) {
  117. if (listener != null) {
  118. listener.onLetterUpdate(LETTERS[index]);
  119. }
  120. Log.d(TAG, "onTouchEvent: " + LETTERS[index]);
  121. mLastTouchIndex = index;
  122. }
  123. }
  124. break;
  125. case MotionEvent.ACTION_UP:
  126. // 手指抬起的时候重置
  127. mLastTouchIndex = -1;
  128. break;
  129. default:
  130. break;
  131. }
  132. //调用这个方法会重新调用draw方法,重新绘制
  133. invalidate();
  134. return true;
  135. }
  136. /**
  137. * 当大小 改变的时候会回调这个方法,
  138. * 这里我们就不主动调用measure()方法了
  139. *
  140. * @param w
  141. * @param h
  142. * @param oldw
  143. * @param oldh
  144. */
  145. @Override
  146. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  147. super.onSizeChanged(w, h, oldw, oldh);
  148. // 获取单元格的宽和高
  149. cellWidth = getMeasuredWidth();
  150. int mHeight = getMeasuredHeight();
  151. cellHeight = mHeight * 1.0f / LETTERS.length;
  152. }
  153. }

代码 解析

  1. public QuickIndexBar(Context context, AttributeSet attrs, int defStyle) {
  2. super(context, attrs, defStyle);
  3. mContext = context;
  4. //初始化自定义属性
  5. obtainAttrs(attrs);
  6. // 初始化画笔
  7. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  8. float textSize = UIUtils.dip2px(15, mContext);
  9. mPaint.setTextSize(textSize);
  10. mPaint.setTypeface(Typeface.DEFAULT_BOLD);
  11. }
  12. private void obtainAttrs(AttributeSet attrs) {
  13. TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.QuickIndexBar);
  14. int selectColor = typedArray.getColor(R.styleable.QuickIndexBar_select_color, -1);
  15. if (selectColor != -1) {
  16. mSelectColor = selectColor;
  17. }
  18. int normalColor = typedArray.getColor(R.styleable.QuickIndexBar_normal_color, -1);
  19. if (normalColor != -1) {
  20. mNormalColor = normalColor;
  21. }
  22. typedArray.recycle();
  23. }
  1. 接着我们在onSizeChange 方法里面拿到我们时间的宽度和高度,有人可能会问了为什么不在onMeasure里面获取了,其实在onMeasure方法里面获取是可以的,只不过我们还需要调用一下measure方法而已,在onSizeChnage方法里面,我们直接调用
  1. cellWidth = getMeasuredWidth();

即可获取到我们需要的宽度和高度。用起来比较方便,不过更多的是为了让大家知道View有这一个方法存在以及怎么使用它
顺便我们来看一下google官方对onSizeChange方法的解释

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

从官方的解释我们可以知道这个方法是在onLayout方法中当大小改变的时候会调用这个方法,因此我们直接调用getMeasuredWidth();是可以获取得到宽度的,因为onMeasure 是先于onLayout方法调用的。
3. 接着我们重写onDraw方法,在onDraw方法我们所做的工作就是绘制 我们需要的26个字母

  1. protected void onDraw(Canvas canvas) {
  2. for (int i = 0; i < LETTERS.length; i++) {
  3. String text = LETTERS[i];
  4. // 计算坐标
  5. int x = (int) (cellWidth / 2.0f - mPaint.measureText(text) / 2.0f);
  6. // 获取文本的高度
  7. Rect bounds = new Rect();// 矩形
  8. mPaint.getTextBounds(text, 0, text.length(), bounds);
  9. int textHeight = bounds.height();
  10. int y = (int) (cellHeight / 2.0f + textHeight / 2.0f + i * cellHeight);
  11. // 根据按下的字母, 设置画笔颜色
  12. mPaint.setColor(mLastTouchIndex == i ? mSelectColor : mNormalColor);
  13. // 绘制文本A-Z
  14. canvas.drawText(text, x, y, mPaint);
  15. }
  16. }
  1. 讲到这里,我们的工作已经完成一大半了,接着就是处理我们是按下或者一个字母了,我们重写onTouchEvent方法,并且return true;是为了保证 Action_down动作按下以后,Action_move以后的动作能够顺利接受到,这涉及到View的事件分发机制,有空的话我会尝试总结一下,这里就不说了
  1. // 获取当前触摸到的字母索引
  2. index = (int) (event.getY() / cellHeight);

同时我们记录下我们当前是触摸或者按下哪一个字母

  1. mLastTouchIndex = index;
  1. 知道了我们当前是触摸或者按下哪一个字母了,那我们要怎样将这些信息暴露出去了,不难想象就是采用接口回调的方法,为此我们提供了这样一个接口
  1. /**
  2. * 暴露一个字母的监听
  3. */
  4. public interface OnLetterUpdateListener {
  5. void onLetterUpdate(String letter);
  6. }

并且提供了设置监听器的方法,这样我们就成功将我们的按下字母的信息提供给外界了

  1. public void setListener(OnLetterUpdateListener listener) {
  2. this.listener = listener;
  3. }

详细代码如下

  1. int index = -1;
  2. switch (MotionEventCompat.getActionMasked(event)) {
  3. case MotionEvent.ACTION_DOWN:
  4. // 获取当前触摸到的字母索引
  5. index = (int) (event.getY() / cellHeight);
  6. if (index >= 0 && index < LETTERS.length) {
  7. // 判断是否跟上一次触摸到的一样,不一样才进行回调
  8. if (index != mLastTouchIndex) {
  9. if (listener != null) {
  10. //
  11. listener.onLetterUpdate(LETTERS[index]);
  12. }
  13. Log.d(TAG, "onTouchEvent: " + LETTERS[index]);
  14. //记录上一次触摸的Index为当前的index;
  15. mLastTouchIndex = index;
  16. }
  17. }
  18. break;
  19. case MotionEvent.ACTION_MOVE:
  20. index = (int) (event.getY() / cellHeight);
  21. if (index >= 0 && index < LETTERS.length) {
  22. // 判断是否跟上一次触摸到的一样
  23. if (index != mLastTouchIndex) {
  24. if (listener != null) {
  25. listener.onLetterUpdate(LETTERS[index]);
  26. }
  27. Log.d(TAG, "onTouchEvent: " + LETTERS[index]);
  28. mLastTouchIndex = index;
  29. }
  30. }
  31. break;
  32. case MotionEvent.ACTION_UP:
  33. // 手指抬起的时候重置
  34. mLastTouchIndex = -1;
  35. break;
  36. default:
  37. break;
  38. }
  39. //调用这个方法会重新调用draw方法,重新绘制
  40. invalidate();
  41. return true;

到此 QuickBarView的源码分析为止,下面我们来学习一下是怎样结合ListView使用的


下面我贴出核心代码,想仔细了解的请点击源码下载 源码下载地址:

  1. mQuickIndexBar.setListener(new OnLetterUpdateListener() {
  2. @Override
  3. public void onLetterUpdate(String letter) {
  4. // UIUtils.showToast(getApplicationContext(), letter);
  5. showLetter(letter);
  6. // 根据字母定位ListView, 找到集合中第一个以letter为拼音首字母的对象,得到索引
  7. for (int i = 0; i < persons.size(); i++) {
  8. Person person = persons.get(i);
  9. String l = person.getPinyin().charAt(0) + "";
  10. if (TextUtils.equals(letter, l)) {
  11. // 匹配成功
  12. mListView.setSelection(i);
  13. break;
  14. }
  15. }
  16. }
  17. });

思路解析 如下
1. 在我们的List数据里面查找是否有有相应的首字母是触摸的字母,有的话返回相应的index,
2. 然后再调用ListView的setSelection(i)方法选中哪一个Item

  1. mListView.setSelection(i);

至于有些item有显示字母,有一些没有显示字母,其实就是判断上一个item的首字母是不是跟当前的首字母是不是一样的,不一样的话,显示当前item的字母,不过要注意一点,就是position等于0的时候,我们需要做特殊处理,代码如下

  1. String str = null;
  2. String currentLetter = p.getPinyin().charAt(0) + "";
  3. // 根据上一个首字母,决定当前是否显示字母
  4. if(position == 0){
  5. str = currentLetter;
  6. }else {
  7. // 上一个人的拼音的首字母
  8. String preLetter = persons.get(position - 1).getPinyin().charAt(0) + "";
  9. if(!TextUtils.equals(preLetter, currentLetter)){
  10. str = currentLetter;
  11. }
  12. }
  13. // 根据str是否为空,决定是否显示索引栏
  14. mViewHolder.mIndex.setVisibility(str == null ? View.GONE : View.VISIBLE);

到此我们的分析为止

转载请注明原博客地址:http://blog.csdn.net/gdutxiaoxu/article/details/51804865
源码下载地址:https://github.com/gdutxiaoxu/QuickIndex.git

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注