Android联系人右侧滚动条

这篇博客介绍如何创建一个自定义View,用于在Android应用中实现类似联系人应用的侧滑字母滚动条。通过绘制所有字母并处理触摸事件,此View可无缝集成到需要的地方。

主要是是自定义View将所有字母画出来,然后对dispatchTouchEvent 处理滑动事件,具体看代码吧!


public class MyLetterView extends View {
    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 int mWidth = 0;
    private int mHeight = 0;

    private int normalTextSize = 22;
    private int normalTextColor;
    private int pressedTextColor;
    private int normalBgColor;
    private int pressedBgColor;
    private boolean pressed = false;

    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;

    public MyLetterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyLetterView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        normalTextColor = context.getResources().getColor(R.color.blue_normal);
        pressedTextColor = context.getResources().getColor(R.color.blue_pressed);
        normalBgColor = context.getResources().getColor(R.color.white_color);
        pressedBgColor = context.getResources().getColor(R.color.white_color);

        this.setBackgroundColor(normalBgColor);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint = reset(paint);

        if (pressed) {
            canvas.drawColor(pressedBgColor);
        }

        int singleHeight = mHeight / b.length;//每个字母范围所占的高度

        for (int i = 0; i < b.length; i++) {
            paint.setColor(normalTextColor);

            if (i == choose) {
                paint.setColor(pressedTextColor);
                paint.setFakeBoldText(true);
            }

            //写字的 坐标
            float xPos = (mWidth - paint.measureText(b[i])) / 2;//横向居中
            float yPos = singleHeight * i + singleHeight;//纵向居中
            canvas.drawText(b[i], xPos, yPos, paint);
        }
    }

    /**
     * 重置画笔
     *
     * @param paint
     * @return
     */
    private Paint reset(Paint paint) {
        //配置画笔
        paint.setAntiAlias(true);
        paint.setTextSize(normalTextSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        return paint;
    }


    @Override
    public 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:
                pressed = true;
                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:
                pressed = false;
                choose = -1;
                invalidate();
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String s);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = this.getWidth();
        mHeight = this.getHeight();
    }
}
效果


然后view放到你需要的地方就能使用啦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值