如下是快速索引的效果图,是从网上下的实例。如图实现的难点1:是最右侧的索引是用自定义View来实现的,主要通过onDraw的方法将其画出;难点2:是如何拿到每个名字的首字母用的是pinyin4j-2.5.0.jar 将汉字转化成拼音再去第一个字符;难点3:ListView的 ...
如下是快速索引的效果图,是从网上下的实例。如图实现的难点1:是最右侧的索引是用自定义View来实现的,主要通过onDraw的方法将其画出;
难点2:是如何拿到每个名字的首字母用的是pinyin4j-2.5.0.jar 将汉字转化成拼音再去第一个字符;难点3:ListView的adapte不好实现
下图的布局是一个ListView右侧是一个自定义的View,中间是一个TextView点击的时候显示
如下是自定义的View来实现快速索引
1 package com.demo.sb.widget; 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.Rect; 8 import android.graphics.Typeface; 9 import android.util.AttributeSet; 10 import android.util.Log; 11 import android.view.MotionEvent; 12 import android.view.View; 13 14 /** 15 * 快速索引 用于根据字母快速定位联系人 也就是界面最右边的那个红色竖条 16 * 17 * @author Administrator 18 * 19 */ 20 public class QuickIndexBar extends View { 21 22 private static final String[] LETTERS = new String[] { "A", "B", "C", "D", 23 "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", 24 "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; 25 26 private Paint mPaint; 27 28 private float cellHeight; 29 30 private int cellWidth; 31 32 /** 33 * 暴露一个字母的监听 34 */ 35 public interface OnLetterUpdateListener { 36 void onLetterUpdate(String letter); 37 } 38 39 private OnLetterUpdateListener listener; 40 41 public OnLetterUpdateListener getListener() { 42 return listener; 43 } 44 45 /** 46 * 设置字母更新的监听 47 */ 48 public void setListener(OnLetterUpdateListener listener) { 49 this.listener = listener; 50 } 51 52 public QuickIndexBar(Context context) { 53 this(context, null); 54 // TODO Auto-generated constructor stub 55 } 56 57 public QuickIndexBar(Context context, AttributeSet attrs) { 58 this(context, attrs, 0); 59 // TODO Auto-generated constructor stub 60 } 61 62 public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) { 63 super(context, attrs, defStyleAttr); 64 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 65 mPaint.setColor(Color.WHITE); 66 mPaint.setTypeface(Typeface.DEFAULT_BOLD); 67 } 68 69 @Override 70 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 71 // TODO Auto-generated method stub 72 super.onSizeChanged(w, h, oldw, oldh); 73 cellWidth = getMeasuredWidth(); 74 75 int mHeight = getMeasuredHeight(); 76 cellHeight = mHeight * 1.0f / LETTERS.length; 77 } 78 79 /** 80 * 绘制自定义最重要的一步重写onDraw方法 画什么,由Canvas处理 怎么画,由Paint处理 81 */ 82 @Override 83 protected void onDraw(Canvas canvas) { 84 // TODO Auto-generated method stub 85 for (int i = 0; i < LETTERS.length; i++) { 86 String text = LETTERS[i]; 87 // 计算坐标 88 int x = (int) (cellWidth / 2.0f - mPaint.measureText(text) / 2.0f); 89 // 获取文本的高度 90 Rect bounds = new Rect();// 矩形 91 mPaint.getTextBounds(text, 0, text.length(), bounds); 92 int textHeight = bounds.height(); 93 int y = (int) (cellHeight / 2.0f + textHeight / 2.0f + i 94 * cellHeight); 95 96 // 根据按下的字母,设置画笔颜色 97 mPaint.setColor(touchIndex == i ? Color.GRAY : Color.WHITE); 98 99 // 绘制文本A-Z100 canvas.drawText(text, x, y, mPaint);101 }102 }103 104 int touchIndex = -1;105 106 @Override107 public boolean onTouchEvent(MotionEvent event) {108 // TODO Auto-generated method stub109 int index = -1;110 switch (event.getAction()) {111 case MotionEvent.ACTION_DOWN:112 // 获取当前触摸到的字母索引113 index = (int) (event.getY() / cellHeight);114 if (index >= 0 && index < LETTERS.length) {115 // 判断是否跟上一次触摸到的一样116 if (index != touchIndex) {117 118 if (listener != null) {119 listener.onLetterUpdate(LETTERS[index]);120 }121 122 Log.d("jiejie", "onTouchEvent: " + LETTERS[index]);123 124 touchIndex = index;125 }126 }127 break;128 case MotionEvent.ACTION_MOVE:129 130 index = (int) (event.getY() / cellHeight);131 if (index >= 0 && index < LETTERS.length) {132 // 判断是否跟上一次触摸到的一样133 if (index != touchIndex) {134 135 if (listener != null) {136 listener.onLetterUpdate(LETTERS[index]);137 }138 139 Log.d("jiejie", "onTouchEvent : " + LETTERS[index]);140 touchIndex = index;141 }142 }143 break;144 case MotionEvent.ACTION_UP:145 touchIndex = -1;146 break;147 default:148 break;149 }150 // 重新绘制151 invalidate();152 return true;153 }154 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:快速索引
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。