public class SideBar extends View {
public static String[] INDEX_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" };
// public static String[] INDEX_SHOW = { "#", "A", ".", ".", ".", ".", "F",
// ".", ".", ".", ".", "K", ".", ".", ".", ".", "P", ".", ".", ".",
// ".", "U", ".", ".", ".", ".", "Z" };
public static String[] INDEX_SHOW = { "","#","","",
"A", "","", ".", ".", "", "",
"","H", "", "", ".", ".", "", "",
"","O", "","", ".", ".", "", "",
"","U", "", "",".", ".", "", "",
"","Z" };
private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
private List letterList;
private List letterShow;
private int choose = -1;
private Paint paint = new Paint();
private TextView mTextDialog;
public SideBar(Context context) {
this(context, null);
}
public SideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setBackgroundColor(Color.parseColor("#00000000"));
letterList = Arrays.asList(INDEX_STRING);
letterShow = Arrays.asList(INDEX_SHOW);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getHeight();//
int width = getWidth();//
int singleHeight = height / letterShow.size();//
for (int i = 0; i < letterShow.size(); i++) {
paint.setColor(Color.parseColor("#FFFFFF"));
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
paint.setTextSize(26);
//
if (i == choose) {
paint.setColor(Color.parseColor("#4F41FD"));
paint.setTextSize(28);
paint.setFakeBoldText(true);
}
// x - .
float xPos = width / 2 - paint.measureText(letterShow.get(i)) / 2;
float yPos = singleHeight * i + singleHeight -2;
canvas.drawText(letterShow.get(i), xPos, yPos, paint);
paint.reset();//
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();// y
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * letterShow.size());// y *b b .
switch (action) {
case MotionEvent.ACTION_UP:
setBackgroundColor(Color.parseColor("#00000000"));
choose = -1;
invalidate();
if (mTextDialog != null) {
mTextDialog.setVisibility(View.GONE);
}
break;
default:
setBackgroundColor(Color.parseColor("#00000000"));
if (oldChoose != c) {
if (c >= 0 && c < letterList.size()) {
String alertText = letterList.get(c);
if (listener != null && !"".equals(alertText)) {
listener.onTouchingLetterChanged(alertText);
}
if (mTextDialog != null && !"".equals(alertText)) {
mTextDialog.setText(alertText);
mTextDialog.setVisibility(View.VISIBLE);
}
choose = c;
invalidate();
}
}
break;
}
return true;
}
public void setIndexText(ArrayList indexStrings) {
this.letterList = indexStrings;
invalidate();
}
/**
* SideBar TextView
*
* @param mTextDialog
*/
public void setTextView(TextView mTextDialog) {
this.mTextDialog = mTextDialog;
}
/**
*
*
* @param onTouchingLetterChangedListener
*/
public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
/**
*
*/
public interface OnTouchingLetterChangedListener {
void onTouchingLetterChanged(String s);
}
}