This article shares the specific code of Android custom letter selection sidebar for your reference. The specific content is as follows
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class LetterSideBar extends View { private Paint mPaint; private int color, selectedColor; private float textSize, spacing; private String mChoosing = "Z"; private OnLetterSelectedListener listener; private int width, height; 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"}; public interface OnLetterSelectedListener { //Number means unchecked void onSelected(String letter); } public void setOnLetterSelectedListener(OnLetterSelectedListener listener) { = listener; } public LetterSideBar(Context context) { this(context, null); } public LetterSideBar(Context context, @Nullable AttributeSet attrs) { super(context, attrs); if(null != attrs) { TypedArray ta = (attrs, ); color = (.LetterSideBar_LetterSideBar_textColor, ); selectedColor = (.LetterSideBar_LetterSideBar_textSelectedColor, ); textSize = (.LetterSideBar_LetterSideBar_textSize, sp2px(12)); spacing = (.LetterSideBar_LetterSideBar_spacing, dp2px(5)); (); } init(); } private void init() { mPaint = new Paint(); (true); (color); (textSize); } @Override protected void onDraw(Canvas canvas) { drawText(canvas); drawSelectedText(canvas, mChoosing); } private void drawText(Canvas canvas) { (color); for (int i=0; i<; i++) { drawLetterAt(canvas, i, LETTERS[i]); } } private void drawSelectedText(Canvas canvas, String selected) { if((selected)) return; (selectedColor); int position = -1; for(int i=0; i<; i++) { if((LETTERS[i])) { position = i; break; } } if(position < 0 || position >= ) return; drawLetterAt(canvas, position, selected); } @Override public boolean onTouchEvent(MotionEvent event) { int action = (); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: float x = (); float y = (); if(isTouchInsideView(x, y)) { //Touch in the control int position = caculatePosition(y); if(position >= 0 && position < ) { //Compliance Position String letter = LETTERS[position]; if(!(mChoosing)) { //Does not match the selected one. Go to refresh the control mChoosing = letter; performListener(mChoosing); invalidate(); } } else { //Not compliant position if(null != mChoosing) { mChoosing = null; performListener(mChoosing); invalidate(); } } } else if(null != mChoosing) { //The click event is not inside the view mChoosing = null; performListener(mChoosing); invalidate();//Touch outside the view Uncheck } return true; default: if(mChoosing != null) { mChoosing = null; performListener(mChoosing); invalidate(); } break; } return (event); } private void performListener(String letter) { if(null != listener) (letter); } private boolean isTouchInsideView(float x, float y) { //The left and right can be properly judged within the control if(x >= 0 && x <= width && y >= getPaddingTop() && y < height) return true; return false; } /** * Calculate the touch location * @param y * @return */ private int caculatePosition(float y) { float heightWithOutPadding = height - getPaddingTop() - getPaddingBottom(); float eachElementHeight = heightWithOutPadding / ; y -= getPaddingTop(); int position = (int) (y / eachElementHeight); return position; } private void drawLetterAt(Canvas canvas, int position, String letter) { float heightForEach = ((height * 1f - getPaddingTop() - getPaddingBottom()) - ( - 1) * spacing) / ; float spacingInUp = spacing * (position - 1); if(spacingInUp < 0) spacingInUp = 0; float currentTop = getPaddingTop() + (heightForEach * position) + spacingInUp; float currentBottom = currentTop + heightForEach; fmi = (); float x = (width - getPaddingLeft() - getPaddingRight() - (letter)) / 2f + getPaddingLeft(); float baseLine = ( + ()) / 2f - ; float y = (currentBottom + currentTop) / 2f + baseLine; (letter, 0, 1, x, y, mPaint); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { (changed, left, top, right, bottom); if(changed) { width = getWidth(); height = getHeight(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { (widthMeasureSpec, heightMeasureSpec); int textWidth = (int) (getPaddingLeft() + getPaddingRight() + ("A")); Rect textBounds = new Rect(); ("A", 0, 1, textBounds); int singleTextHeight = (); int totalHeight = (int) (27f * singleTextHeight + 26f * spacing) + getPaddingBottom() + getPaddingTop();//26 letters + 1# int widthMode = (widthMeasureSpec); int heightMode = (widthMeasureSpec); int specWidth = (widthMeasureSpec); int specHeight = (widthMeasureSpec); int realWidth, realHeight; if(widthMode == ) { realWidth = specWidth; } else { realWidth = textWidth; } if(heightMode == ) { realHeight = specHeight; } else { realHeight = totalHeight; } setMeasuredDimension(realWidth, realHeight); } protected int dp2px(int dp) { return (int) (getContext().getResources().getDisplayMetrics().density * dp + 0.5); } protected int sp2px(int sp) { return (int) (getContext().getResources().getDisplayMetrics().scaledDensity * sp + 0.5); } }
<declare-styleable name="LetterSideBar"> <attr name="LetterSideBar_textColor" format="color"/> <attr name="LetterSideBar_textSelectedColor" format="color"/> <attr name="LetterSideBar_textSize" format="dimension"/> <attr name="LetterSideBar_spacing" format="dimension"/> </declare-styleable>
< android: android:layout_width="wrap_content" android:layout_height="wrap_content" app:LetterSideBar_textSize="14sp" app:LetterSideBar_textColor="#FFFFFF" android:padding="10dp" app:LetterSideBar_textSelectedColor="#FF0000" app:LetterSideBar_spacing="2dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:background="#A4A4A4"/>
Used in the code
(new () { @Override public void onSelected(String letter) { if((letter)) { ("Uncheck"); (); } else { ("Selected" + letter); (letter); (); } } });
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.