字母索引表

来源:互联网 发布:成都妇女儿童医院网络 编辑:程序博客网 时间:2024/06/06 15:01

字母索引表

第一种

package com.example.mio.myapplication;/** * Created by mio on 2017/1/26. */import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.TextView;import java.util.List;public class LetterIndex extends View {    private Paint mPaint = null;    private String[] arrLetters = new 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"};    private int lineHeight = 0;    private TextView alert_textView;    private OnLetterClickedListener listener;    private int changePainColor = -1;    public LetterIndex(Context context) {        super(context);    }    public LetterIndex(Context context, AttributeSet attrs) {        super(context, attrs);        mPaint = new Paint();        mPaint.setAntiAlias(true);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        float density = getResources().getDisplayMetrics().density;        mPaint.setTextSize(14 * density);        int viewHeight = getHeight();        int viewWidth = getWidth();        //每一行的高度        lineHeight = viewHeight / arrLetters.length;        for (int i = 0; i < arrLetters.length; i++) {            mPaint.setColor(Color.GRAY);            //获取每个字体的宽度            int textWidth = (int) mPaint.measureText(arrLetters[i]);            //在画布上绘制对应的字体            canvas.drawText(arrLetters[i], (viewWidth - textWidth) / 2, (i + 1) * lineHeight,                    mPaint);        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        float y = event.getY();        //获取滑动的位置        int position = (int) (y / lineHeight);        if (position >= 0 && position < arrLetters.length) {            switch (event.getAction()) {                case MotionEvent.ACTION_UP:                    setBackgroundColor(Color.TRANSPARENT);                    if (alert_textView != null && alert_textView.getVisibility() == View.VISIBLE) {                        alert_textView.setVisibility(GONE);                    }                    invalidate();                    break;                default:                    setBackgroundColor(Color.parseColor("#cccccc"));                    if (alert_textView != null) {                        alert_textView.setVisibility(View.VISIBLE);                        alert_textView.setText(arrLetters[position]);                    }                    //为监听器中的抽象方法的参数进行赋值                    if (listener != null) {                        listener.onLetterClicked(arrLetters[position]);                    }                    //擦除当前View,重新进行绘制                    invalidate();                    break;            }        }        return true;    }    //定义抽象方法,    public interface OnLetterClickedListener {        void onLetterClicked(String letter);    }    //自定义方法。用于调用者为指定参数进行赋值    public void setOnLetterClickedListener(TextView alert_textView, OnLetterClickedListener            listener) {        this.listener = listener;        this.alert_textView = alert_textView;    }}

第二种

package com.example.mio.myapplication;import android.view.View;/** * Created by mio on 2017/1/26. */import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.TextView;/** * Created by qdhhh on 2016/10/10. */public class LetterIndexSideBar extends View {    //当前手指滑动到的位置    private int choosedPosition = -1;    //画文字的画笔    private Paint paint;    //右边的所有文字    private String[] letters = new 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"};    //页面正中央的TextView,用来显示手指当前滑动到的位置的文本    private TextView textViewDialog;    //接口变量,该接口主要用来实现当手指在右边的滑动控件上滑动时ListView能够跟着滚动    private UpdateListView updateListView;    public LetterIndexSideBar(Context context) {        this(context, null);    }    public LetterIndexSideBar(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public LetterIndexSideBar(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        paint = new Paint();        paint.setAntiAlias(true);        paint.setTextSize(24);    }    /**     * 设置滑动侧边栏的时候在屏幕中央显示的当前字母索引的提醒     *     * @param textViewDialog     */    public void setTextViewDialog(TextView textViewDialog) {        this.textViewDialog = textViewDialog;    }    @Override    protected void onDraw(Canvas canvas) {        int perTextHeight = getHeight() / letters.length;        for (int i = 0; i < letters.length; i++) {            if (i == choosedPosition) {                paint.setColor(Color.RED);            } else {                paint.setColor(Color.BLACK);            }            canvas.drawText(letters[i], (getWidth() - paint.measureText(letters[i])) / 2, (i + 1) * perTextHeight, paint);        }    }    /**     * 设置手势在控件上滑动的时候的响应     *     * @param event     * @return     */    @Override    public boolean onTouchEvent(MotionEvent event) {        int perTextHeight = getHeight() / letters.length;        float y = event.getY();        int currentPosition = (int) (y / perTextHeight);        /**         * currentPosition不能为小于0的值         */        if (currentPosition < 0) {            currentPosition = 0;        } else if (currentPosition >= letters.length) {            currentPosition = letters.length - 1;        }        String letter = letters[currentPosition];        switch (event.getAction()) {            case MotionEvent.ACTION_UP:                setBackgroundColor(Color.TRANSPARENT);                if (textViewDialog != null) {                    textViewDialog.setVisibility(View.GONE);                }                break;            default:                setBackgroundColor(Color.parseColor("#cccccc"));                if (currentPosition > -1 && currentPosition < letters.length) {                    if (textViewDialog != null) {                        textViewDialog.setVisibility(View.VISIBLE);                        textViewDialog.setText(letter);                    }                    if (updateListView != null) {                        updateListView.updateListView(letter);                    }                    choosedPosition = currentPosition;                }                break;        }        invalidate();        return true;    }    public void setUpdateListView(UpdateListView updateListView) {        this.updateListView = updateListView;    }    /**     * 一个接口 ,实现后用于在滑动这个自定义view的时候listview也会随之发生滑动     */    public interface UpdateListView {        public void updateListView(String currentChar);    }    /**     * 滑动listview的时候这个自定义的view中的字母索引之也会随之发生改变     *     * @param currentChar     */    public void updateLetterIndexView(int currentChar) {        for (int i = 0; i < letters.length; i++) {            if (currentChar == letters[i].charAt(0)) {                choosedPosition = i;                invalidate();                break;            }        }    }}

第三种 (推荐)

package com.example.mio.myapplication;/** * Created by mio on 2017/1/26. */import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.TypedValue;import android.view.MotionEvent;import android.view.View;import android.widget.TextView;/** * Created by liyunkun on 2016/9/27 0027. */public class LetterIndexView extends View {    private String[] letters = new 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", "#"};    private Paint paint;    private int currentPosition = 0;    private TextView showLetterTv;    public void setShowLetterTv(TextView showLetterTv) {        this.showLetterTv = showLetterTv;    }    public LetterIndexView(Context context) {        this(context, null);    }    public LetterIndexView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public LetterIndexView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        paint = new Paint();        paint.setAntiAlias(true);        paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()));        paint.setTextAlign(Paint.Align.CENTER);    }    @Override    protected void onDraw(Canvas canvas) {        int height = getMeasuredHeight();        int width = getMeasuredWidth();        int letterHeight = height / letters.length;        for (int i = 0; i < letters.length; i++) {            String letter = letters[i];            if (currentPosition == i) {                paint.setColor(Color.RED);            } else {                paint.setColor(Color.parseColor("#55000000"));            }            canvas.drawText(letter, width / 2, (i + 1) * letterHeight, paint);        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int height = getMeasuredHeight();        float y = event.getY();        int letterHeight = height / letters.length;        int action = event.getAction();        currentPosition = (int) (y / letterHeight);        switch (action) {            case MotionEvent.ACTION_DOWN:            case MotionEvent.ACTION_MOVE:                setBackgroundColor(Color.parseColor("#11000000"));                if (showLetterTv != null) {                    if (currentPosition > -1 && currentPosition < letters.length) {                        showLetterTv.setVisibility(VISIBLE);                        showLetterTv.setText(letters[currentPosition]);                        if (updateListViewItem != null) {                            updateListViewItem.updateListViewItem(letters[currentPosition].charAt(0));                        }                    }                }                break;            case MotionEvent.ACTION_UP:                setBackgroundColor(Color.TRANSPARENT);                if (showLetterTv != null) {                    showLetterTv.setVisibility(GONE);                }                break;        }        invalidate();        return true;    }    public interface UpdateListViewItem {        void updateListViewItem(int selection);    }    private UpdateListViewItem updateListViewItem;    public void setUpdateListViewItem(UpdateListViewItem updateListViewItem) {        this.updateListViewItem = updateListViewItem;    }    public void updateLetterArray(int position) {        for (int i = 0; i < letters.length; i++) {            if (letters[i].charAt(0) == position) {                currentPosition = i;                invalidate();            }        }    }}

第四种

package com.shixin.diyview;import java.util.ArrayList;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;/** * 字母索引表 * @author mio */public class CustomLetterBar extends View {    private Paint mPaint;    private ArrayList<String> letterList;    private float dy;    private float dx;    private int index;    private Paint paint;    private boolean isTouch;    // 用于获取当前的文本内容    public String getLetterText() {        String s = letterList.get(index);        return s;    }    public CustomLetterBar(Context context, AttributeSet attrs) {        super(context, attrs);        mPaint = new Paint();        // mPaint.setAntiAlias(true);        mPaint.setStyle(Paint.Style.FILL);        mPaint.setStrokeWidth(2);        mPaint.setColor(Color.RED);        // 设置绘制文本大小        mPaint.setTextSize(30);        paint = new Paint();        // mPaint.setAntiAlias(true);        paint.setStyle(Paint.Style.FILL);        paint.setStrokeWidth(2);        paint.setColor(Color.GREEN);        // 设置绘制文本大小        paint.setTextSize(30);        letterList = new ArrayList<String>();        letterList.add("#");        for (int i = 'A'; i <= 'Z'; i++) {            char a = (char) i;            letterList.add(a + "");        }    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        dx = canvas.getWidth();        dy = canvas.getHeight();        // 移动画布        canvas.translate(dx / 2, 0);        for (int i = 0; i < letterList.size(); i++) {            canvas.drawText(letterList.get(i), 0,                    dy / letterList.size() * i + 20, mPaint);        }        if (isTouch) {            canvas.drawText(letterList.get(index), 0,                    dy / letterList.size() * index + 20, paint);        }    }    /**     * 触碰LetterBar时触发     */    @Override    public boolean onTouchEvent(MotionEvent event) {        // 用户触碰Letter的高度        float y2 = event.getY();        index = (int) (y2 / (dy / letterList.size()));        // Log.e("index", y2 + "");        invalidate();        if (index > letterList.size() - 1) {            index = letterList.size() - 1;        } else if (index < 0) {            index = 0;        }        // 获取用户的动作        int action = event.getAction();        switch (action) {        case MotionEvent.ACTION_DOWN:            // 需要判断接口对象是否为空否则会闪退            if (listener != null) {                listener.onTouchDown();            }            // Log.e("ACTION_DOWN", "按下");            isTouch = true;            break;        case MotionEvent.ACTION_MOVE:            if (listener != null) {                listener.onTouchDown();            }            // Log.e("ACTION_MOVE", "移动");            isTouch = true;            break;        case MotionEvent.ACTION_UP:            if (listener != null) {                listener.onTouchUp();            }            // Log.e("ACTION_UP", "放开");            isTouch = false;            break;        default:            break;        }        // Toast.makeText(getContext(), "点击了", Toast.LENGTH_SHORT).show();        // 取消拦截        return true;    }    // 1.设置监听的方法    public void setOnTouchLetterListener(OnTouchLetterListener l) {        if (l != null) {            listener = l;        }    }    public int getIndex() {        return index;    }    public void setIndex(int index) {        this.index = index;    }    public ArrayList<String> getLetterList() {        return letterList;    }    public void setLetterList(ArrayList<String> letterList) {        this.letterList = letterList;    }    // 2.定义一个接口    public interface OnTouchLetterListener {        // 监听按下和移动        void onTouchDown();        // 监听离开        void onTouchUp();    }    // 3.定义一个变量    private OnTouchLetterListener listener;}
0 0
原创粉丝点击