右侧A-Z导航

来源:互联网 发布:淘宝领取次数已用完 编辑:程序博客网 时间:2024/04/28 18:14
package com.bitcare.view;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;/** * @date 2013-8-6 */public class MySideBar extends View {OnTouchingLetterChangedListener onTouchingLetterChangedListener;// 26个字母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" };int choose = -1;Paint paint = new Paint();public MySideBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public MySideBar(Context context, AttributeSet attrs) {super(context, attrs);}public MySideBar(Context context) {super(context);}/** * 重写这个方法 */protected void onDraw(Canvas canvas) {super.onDraw(canvas);int height = getHeight();int width = getWidth();int singleHeight = height / b.length;for (int i = 0; i < b.length; i++) {paint.setColor(Color.BLACK);paint.setTypeface(Typeface.DEFAULT_BOLD);paint.setAntiAlias(true);paint.setTextSize(20);if (i == choose) {paint.setColor(Color.RED);paint.setFakeBoldText(true);}float xPos = width / 2 - paint.measureText(b[i]) / 2;float yPos = singleHeight * i + singleHeight;canvas.drawText(b[i], xPos, yPos, paint);paint.reset();}}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {final int action = event.getAction();final float y = event.getY();final int oldChoose = choose;final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;final int c = (int) (y / getHeight() * b.length);switch (action) {case MotionEvent.ACTION_DOWN:if (oldChoose != c && listener != null) {if (c > 0 && c < b.length) {listener.onTouchingLetterChanged(b[c]);choose = c;invalidate();}}break;case MotionEvent.ACTION_MOVE:if (oldChoose != c && listener != null) {if (c > 0 && c < b.length) {listener.onTouchingLetterChanged(b[c]);choose = c;invalidate();}}break;case MotionEvent.ACTION_UP:listener.onTouchingUp();choose = -1;invalidate();break;}return true;}@Overridepublic boolean onTouchEvent(MotionEvent event) {return super.onTouchEvent(event);}/** * 向外公开的方法 *  * @param onTouchingLetterChangedListener */public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;}/** * 接口 */public interface OnTouchingLetterChangedListener {public void onTouchingLetterChanged(String s);//改变事件public void onTouchingUp();//抬手事件}}
Activity中的代码
TextView overlay;// 显示选中哪个字母private void initOverlay() {overlay = new TextView(context);overlay.setGravity(Gravity.CENTER);overlay.setBackgroundResource(R.color.white);overlay.setTextColor(getResources().getColor(R.color.blue_side_txt));overlay.setTextSize(70);overlay.setWidth(140);overlay.setHeight(140);overlay.setVisibility(View.INVISIBLE);WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);windowManager.addView(overlay, lp);}
在onCreate中使用该方法,初始化弹出的

private OnTouchingLetterChangedListener changedListener = new OnTouchingLetterChangedListener() {@Overridepublic void onTouchingLetterChanged(String s) {overlay.setText(s);overlay.setVisibility(View.VISIBLE);if (firstAlphabet.get(s) != null) {lvCity.setSelection(firstAlphabet.get(s));}}@Overridepublic void onTouchingUp() {overlay.setVisibility(View.INVISIBLE);}};