1 package com.xqx.SideBarDemo;
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 import android.widget.TextView;
11
12
13 public class SideBar extends View {
14
15 public String[] characters = new String[] { "#", "A", "B", "C", "D", "E", "F",
16 "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
17 "X", "Y", "Z" };
18
19 private Paint paint;
20 private int textSize = 20; //拼音文字大小
21 private int defaultTextColor = //默认拼音文字的颜色 Color.parseColor("#D2D2D2");
22 private int selectedTextColor = //选中后的拼音文字的颜色 Color.parseColor("#2DB7E1");
23 private int touchedBgColor = //触摸时的拼音文字的颜色 Color.parseColor("#F5F5F5");
24 private TextView text_dialog;
25
26 private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
27
28 private int position = -1;
29
30 public SideBar(Context context) {
31 super(context);
32 }
33
34 public SideBar(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 init();
37 }
38
39 public SideBar(Context context, AttributeSet attrs, int defStyle) {
40 super(context, attrs, defStyle);
41 init();
42 }
43
44 public void setTextDialog(TextView textView){
45 this.text_dialog = textView;
46 }
47
48 private void init() {
49 paint = new Paint();
50 paint.setAntiAlias(true);
51 }
52
53 @Override
54 protected void onDraw(Canvas canvas) {
55 super.onDraw(canvas);
56
57 int height = getHeight();
58 int width = getWidth();
59 int singleHeight = height / characters.length;
60
61 for (int i = 0; i < characters.length; i++) {
62 if (i == position) {
63 paint.setColor(selectedTextColor);
64 } else {
65 paint.setColor(defaultTextColor);
66 }
67 paint.setTextSize(textSize);
68
69 float xPos = width / 2 - paint.measureText(characters[i]) / 2;
70 float yPos = singleHeight * i + singleHeight;
71 canvas.drawText(characters[i], xPos, yPos, paint);
72 }
73 }
74
75 @Override
76 public boolean onTouchEvent(MotionEvent event) {
77 int action = event.getAction();
78 float y = event.getY();
79 position = (int) (y / (getHeight() / characters.length));
80 if (position >= 0 && position < Constants.CITY_TYPE.length) {
81 onTouchingLetterChangedListener.onTouchingLetterChanged(position);
82 switch (action) {
83 case MotionEvent.ACTION_UP:
84 setBackgroundColor(Color.TRANSPARENT);
85 position = -1;
86 invalidate();
87 if (text_dialog != null) {
88 text_dialog.setVisibility(View.INVISIBLE);
89 }
90 break;
91 default:
92 setBackgroundColor(touchedBgColor);
93 invalidate();
94 text_dialog.setText(characters[position]);
95 break;
96 }
97 }else{
98 setBackgroundColor(Color.TRANSPARENT);
99 if (text_dialog != null) {
100 text_dialog.setVisibility(View.INVISIBLE);
101 }
102
103 }
104 return true;
105 }
106
107 public void setOnTouchingLetterChangedListener(
108 OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
109 this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
110 }
111
112
113 public interface OnTouchingLetterChangedListener {
114 public void onTouchingLetterChanged(int position);
115 }
116
117 }