SideBar 选择城市时右侧边上的 选择bar

        需要定义一个SideBar的视图类  在布局文件中引用   同时在布局中设置一个textView默认不可见 

当触摸时才显示   在调用的Activity中 

sideBar.setOnTouchingLetterChangedListener方法
离开时不可见 同时联动listView listView.setSelection(i)设置联动;

  1 package com.archie.lazyhousewifery.cityandauntdetail.makeview;
  2 
  3 import android.content.Context;
  4 import android.graphics.Canvas;
  5 import android.graphics.Color;
  6 import android.graphics.Paint;
  7 import android.util.AttributeSet;
  8 import android.view.MotionEvent;
  9 import android.view.View;
 10 import android.widget.TextView;
 11 
 12 import com.archie.lazyhousewifery.cityandauntdetail.Constants;
 13 
 14 public class SideBar extends View {
 15 
 16     public String[] characters = new String[] { "#", "A", "B", "C", "D", "E", "F",
 17             "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
 18             "X", "Y", "Z" };
 19 
 20     private Paint paint;
 21     private int textSize = 16;
 22     private int defaultTextColor = Color.parseColor("#D2D2D2");
 23     private int selectedTextColor = Color.parseColor("#2DB7E1");
 24     private int touchedBgColor = Color.parseColor("#F5F5F5");
 25     private TextView  text_dialog;
 26 
 27     private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
 28 
 29     private int position = -1;
 30 
 31     public SideBar(Context context) {
 32         super(context);
 33     }
 34 
 35     public SideBar(Context context, AttributeSet attrs) {
 36         super(context, attrs);
 37         init();
 38     }
 39 
 40     public SideBar(Context context, AttributeSet attrs, int defStyle) {
 41         super(context, attrs, defStyle);
 42         init();
 43     }
 44     
 45     public void setTextDialog(TextView textView){
 46         this.text_dialog = textView;
 47     } 
 48 
 49     private void init() {
 50         paint = new Paint();
 51         paint.setAntiAlias(true);
 52     }
 53 
 54     @Override
 55     protected void onDraw(Canvas canvas) {
 56         super.onDraw(canvas);
 57 
 58         int height = getHeight();
 59         int width = getWidth();
 60         int singleHeight = height / characters.length;
 61 
 62         for (int i = 0; i < characters.length; i++) {
 63             if (i == position) {
 64                 paint.setColor(selectedTextColor);
 65             } else {
 66                 paint.setColor(defaultTextColor);
 67             }
 68             paint.setTextSize(textSize);
 69 
 70             float xPos = width / 2 - paint.measureText(characters[i]) / 2;
 71             float yPos = singleHeight * i + singleHeight;
 72             canvas.drawText(characters[i], xPos, yPos, paint);
 73         }
 74     }
 75 
 76     @Override
 77     public boolean onTouchEvent(MotionEvent event) {
 78         int action = event.getAction();
 79         float y = event.getY();
 80         position = (int) (y / (getHeight() / characters.length));
 81         if (position >= 0 && position < Constants.CITY_TYPE.length) {
 82             onTouchingLetterChangedListener.onTouchingLetterChanged(position);
 83             switch (action) {
 84                 case MotionEvent.ACTION_UP:
 85                     setBackgroundColor(Color.TRANSPARENT);
 86                     position = -1;
 87                     invalidate();
 88                     if (text_dialog != null) {
 89                         text_dialog.setVisibility(View.INVISIBLE);
 90                     }
 91                     break;
 92                 default:
 93                     setBackgroundColor(touchedBgColor);
 94                     invalidate();
 95                     text_dialog.setText(characters[position]);
 96                     break;
 97             }
 98         }else{
 99 
100             setBackgroundColor(Color.TRANSPARENT);
101             if (text_dialog != null) {
102                 text_dialog.setVisibility(View.INVISIBLE);
103             }
104 
105         }
106         return true;
107     }
108 
109     public void setOnTouchingLetterChangedListener(
110             OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
111         this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
112     }
113 
114 
115     public interface OnTouchingLetterChangedListener {
116         public void onTouchingLetterChanged(int position);
117     }
118 
119 }
SideBar
private void initSideBar() {

        sideBar.setTextDialog(dialog_text);
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(int position) {
                String city_label = Constants.CITY_TYPE[position];
                for (int i = 0; i < cityList.size(); i++) {
                    if (cityList.get(i).getCityName().equals(city_label)) {

                        cityListView.setSelection(i);
                        dialog_text.setVisibility(View.VISIBLE);
                        break;
                    }
                    if(i == cityList.size() -1){

                        dialog_text.setVisibility(View.INVISIBLE);

                    }
                }
            }
        });

    }
上边的代码是在activity中引用sideBar  回调的方法  其他的稍作处理就可以运行了




原文地址:https://www.cnblogs.com/bimingcong/p/4945937.html