import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import com.xm.tokenalliance.R;
/**
* 类简要描述
*/
public class SideBar extends View {
// 字母变化监听事件
private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
// 字母数组
public static String[] b = {"*", "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 int choose = -1;
private Paint paint = new Paint();
// 点击后提示当前选中字母
private TextView mTextDialog;
public void setTextView(TextView mTextDialog) {
this.mTextDialog = mTextDialog;
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SideBar(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取屏幕高度和宽度
int height = getHeight();
int width = getWidth();
// 设置字母高度
float letterHeight = (height * 1f) / b.length;
for (int i = 0; i < b.length; i++) {
// paint.setColor(Color.rgb(23, 122, 126));
paint.setColor(getResources().getColor(R.color.color_2982c7));
paint.setTypeface(Typeface.DEFAULT_BOLD);
// 抗锯齿
paint.setAntiAlias(true);
paint.setTextSize(20);
if (i == choose) {
paint.setColor(Color.BLUE);
// 设置为加粗字体
paint.setFakeBoldText(true);
}
// x坐标等于中间-字符串宽度的一半.
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = letterHeight * i + letterHeight;
canvas.drawText(b[i], xPos, yPos, paint);
// 重置画笔
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float touch_y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
final int c = (int) (touch_y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_UP:
setBackgroundColor(Color.TRANSPARENT);
choose = -1;
invalidate();
if (mTextDialog != null) {
mTextDialog.setVisibility(View.INVISIBLE);
}
break;
default:
setBackgroundResource(R.color.holo_green_light);
if (oldChoose != c) {
if (c >= 0 && c < b.length) {
if (listener != null) {
listener.onTouchingLetterChanged(b[c]);
}
if (mTextDialog != null) {
mTextDialog.setText(b[c]);
mTextDialog.setVisibility(View.VISIBLE);
}
choose = c;
invalidate();
}
}
break;
}
return true;
}
/**
* 向外公开的方法
*
* @param onTouchingLetterChangedListener
*/
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
/**
* 接口
*
* @author coder
*/
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}
使用方式:
SideBar sidebar = (SideBar) view.findViewById(R.id.country_sidebar);
sidebar .setTextView(在屏幕中间显示当前选中的字母控件);
// 右侧sideBar监听sidebar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {
// 该字母首次出现的位置
int position = adapter.getPositionForSection(s.charAt(0));
if (position != -1) {
listview.setSelection(position); }
}
});
网友评论