仿Android通讯录右侧的字母索引条,可通过setTextView(TextView LetterNotice)
方法设置选中字母弹出样式,默认不显示。通过setOnTouchBarListener(OnTouchBarListener listener)
响应点击的回调事件。
Android字母索引条
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class AlphabetScrollBar extends View {
private Paint mPaint = new Paint();
private String[] mAlphabet = new String[]{
"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
"#"
};
private boolean mPressed;
private int mCurPosIdx = -1;
private int mOldPosIdx = -1;
private OnTouchBarListener mTouchListener;
private TextView LetterNotice;
public AlphabetScrollBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public AlphabetScrollBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AlphabetScrollBar(Context context) {
this(context, null);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
int singleLetterH = height / mAlphabet.length;
if (mPressed) {
canvas.drawColor(Color.parseColor("#20000000")); //按下背景颜色
}
for (int i = 0; i < mAlphabet.length; i++) {
mPaint.setColor(Color.parseColor("#000000")); //初始字体颜色
mPaint.setAntiAlias(true);
mPaint.setTextSize(24);
float x = width / 2 - mPaint.measureText(mAlphabet[i]) / 2;
float y = singleLetterH * i + singleLetterH - 0.5f;
if (i == mCurPosIdx) {
mPaint.setColor(Color.parseColor("#4682B4")); //设置按下字体颜色变化
mPaint.setFakeBoldText(true);
}
canvas.drawText(mAlphabet[i], x, y, mPaint);
mPaint.reset();
}
}
public boolean onTouchEvent(MotionEvent arg0) {
int action = arg0.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mPressed = true;
mCurPosIdx = (int) (arg0.getY() / this.getHeight() * mAlphabet.length);
if (mTouchListener != null && mOldPosIdx != mCurPosIdx) {
if ((mCurPosIdx >= 0) && (mCurPosIdx < mAlphabet.length)) {
mTouchListener.onTouch(mAlphabet[mCurPosIdx]);
this.invalidate();
}
mOldPosIdx = mCurPosIdx;
}
if (LetterNotice!=null) {
LetterNotice.setText(mAlphabet[mCurPosIdx]);
LetterNotice.getPaint().setFakeBoldText(true);
LetterNotice.setVisibility(View.VISIBLE);
}
return true;
case MotionEvent.ACTION_UP:
if (LetterNotice != null) {
LetterNotice.setVisibility(View.INVISIBLE);
}
mPressed = false;
mCurPosIdx = -1;
this.invalidate();
return true;
case MotionEvent.ACTION_MOVE:
mCurPosIdx = (int) (arg0.getY() / this.getHeight() * mAlphabet.length);
if (mTouchListener != null && mCurPosIdx != mOldPosIdx) {
if ((mCurPosIdx >= 0) && (mCurPosIdx < mAlphabet.length)) {
mTouchListener.onTouch(mAlphabet[mCurPosIdx]);
this.invalidate();
}
mOldPosIdx = mCurPosIdx;
}
if (mCurPosIdx >= 0 && mCurPosIdx < mAlphabet.length) {
if (LetterNotice!=null) {
LetterNotice.setText(mAlphabet[mCurPosIdx]);
LetterNotice.setVisibility(View.VISIBLE);
}
}
return true;
default:
return super.onTouchEvent(arg0);
}
}
public interface OnTouchBarListener {
void onTouch(String letter);
}
public void setOnTouchBarListener(OnTouchBarListener listener) {
mTouchListener = listener;
}
public void setTextView(TextView LetterNotice) {
this.LetterNotice = LetterNotice;
}
}