wjc
2025-04-09 87cd5df70918e6ba1af849c5f026d3719bfdb1ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.sahooz.library.countrypicker;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
import java.util.ArrayList;
import java.util.Arrays;
 
 
/**
 * Created by android on 3/14/2018.
 */
 
public class SideBar extends View {
 
    public final ArrayList<String> indexes = new ArrayList<>();
    private OnLetterChangeListener onLetterChangeListener;
    private Paint paint;
    private float textHeight;
    private int cellWidth;
    private int cellHeight;
    private int currentIndex = -1;
    private int letterColor;
    private int selectColor;
    private int letterSize;
 
    public SideBar(Context context) { this(context, null); }
 
    public SideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public SideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
 
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SideBar, defStyleAttr, 0);
        letterColor = ta.getColor(R.styleable.SideBar_letterColor, Color.BLACK);
        selectColor = ta.getColor(R.styleable.SideBar_selectColor, Color.CYAN);
        letterSize = ta.getDimensionPixelSize(R.styleable.SideBar_letterSize, 24);
        ta.recycle();
        paint = new Paint();
        //消除锯齿
        paint.setAntiAlias(true);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        textHeight = (float) Math.ceil(fontMetrics.descent - fontMetrics.ascent);  //1.1---2   2.1--3
        String[] letters = {"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"};
        indexes.addAll(Arrays.asList(letters));
    }
 
    public void addIndex(String indexStr, int position) {
        indexes.add(position, indexStr);
        invalidate();
    }
 
    public void removeIndex(String indexStr) {
        indexes.remove(indexStr);
        invalidate();
    }
 
    public void setLetterSize(int letterSize) {
        if(this.letterSize == letterSize) return;
        this.letterSize = letterSize;
        invalidate();
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cellWidth = getMeasuredWidth();
        cellHeight = getMeasuredHeight() / indexes.size();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setTextSize(letterSize);
        for (int i = 0; i < indexes.size(); i++) {
            String letter = indexes.get(i);
            float textWidth = paint.measureText(letter);
            float x = (cellWidth - textWidth) * 0.5f;
            float y = (cellHeight + textHeight) * 0.5f + cellHeight * i;
 
            if (i == currentIndex) {
                paint.setColor(selectColor);
            } else {
                paint.setColor(letterColor);
            }
 
            canvas.drawText(letter, x, y, paint);
        }
    }
 
    public OnLetterChangeListener getOnLetterChangeListener() {
        return onLetterChangeListener;
    }
 
    public void setOnLetterChangeListener(OnLetterChangeListener onLetterChangeListener) {
        this.onLetterChangeListener = onLetterChangeListener;
    }
 
    public String getLetter(int position) {
        if(position < 0 || position >= indexes.size()) return "";
        return indexes.get(position);
    }
 
    public interface OnLetterChangeListener {
        void onLetterChange(String letter);
 
        //手指抬起
        void onReset();
    }
 
    /**
     * 处理 按下 移动 手指抬起
     */
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                int downY = (int) event.getY();
                //获取当前索引
                currentIndex = downY / cellHeight;
                if (currentIndex < 0 || currentIndex > indexes.size() - 1) {
 
                } else {
                    if (onLetterChangeListener != null) {
                        onLetterChangeListener.onLetterChange(indexes.get(currentIndex));
                    }
                }
                //重新绘制
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                currentIndex = -1;
                //手动刷新
                invalidate();
                //表示手指抬起了
                if (onLetterChangeListener != null) {
                    onLetterChangeListener.onReset();
                }
                break;
        }
        // 为了 能够接受  move+up事件
        return true;
    }
}