//QuickIndexBar快速索引
public class QuickIndexBar extends View {
private static 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 float cellHeight; // 单元格高度
private int cellWidth; // 单元格宽度
private OnLetterUpdateListener listener;
public QuickIndexBar(Context context) {
this(context, null);
}
public QuickIndexBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 创建抗锯齿的画笔
paint.setColor(Color.RED);
paint.setTextSize(24);
paint.setTypeface(Typeface.DEFAULT_BOLD);// 粗体
}
/**
* 动态更新数据
*
* @param s
*/
public void setLetters(String[] s) {
this.LETTERS = s;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
int height = getHeight();// 获取对应高度
int cellHeight = height / LETTERS.length;// 获取每一个字母的高度
// 绘制字母A-Z
for (int i = 0; i < LETTERS.length; i++) {
String letter = LETTERS[i];
// 计算x值
float x = cellWidth / 2.0f - paint.measureText(letter) / 2.0f;
// 计算y值
// 获取文本的高度
Rect bounds = new Rect();
paint.getTextBounds(letter, 0, letter.length(), bounds);
paint.setColor(getResources().getColor(R.color.color_ee634c));
int textHeight = bounds.height();
float y = cellHeight / 2.0f + textHeight / 2.0f + i * cellHeight;
// 绘制一个文本到界面
canvas.drawText(letter, x, y, paint);
}
}
int currentIndex = -1;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int index = -1;
switch (action) {
case MotionEvent.ACTION_DOWN:
index = (int) (event.getY() / cellHeight);
// System.out.println("event.getY(): " + event.getY());
if (index >= 0 && index < LETTERS.length) {
if (index != currentIndex) {
// System.out.println("index: " + index + "-" + LETTERS[index]);
if (listener != null) {
listener.onLetterUpdate(LETTERS[index]);
}
currentIndex = index;// 记录当前的索引
}
}
break;
case MotionEvent.ACTION_MOVE:
index = (int) (event.getY() / cellHeight);
// System.out.println("event.getY(): " + event.getY());
if (index >= 0 && index < LETTERS.length) {
if (index != currentIndex) {
// System.out.println("index: " + index + "-" + LETTERS[index]);
if (listener != null) {
listener.onLetterUpdate(LETTERS[index]);
}
currentIndex = index;// 记录当前的索引
}
}
break;
case MotionEvent.ACTION_UP:
currentIndex = -1;
break;
}
return true; // 消费触摸事件
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int mHeight = getMeasuredHeight();
// 10 , 3 = 3.33
// cellHeight = mHeight * 1.0f / LETTERS.length;
cellWidth = getMeasuredWidth();
}
public void setOnLetterUpdateListener(OnLetterUpdateListener listener) {
this.listener = listener;
}
public interface OnLetterUpdateListener {
/**
* 字母更新监听
*
* @param letter
*/
void onLetterUpdate(String letter);
}
}
//SelectDepatmentPopupView类
public class SelectDepatmentPopupView extends PopupWindow implements AdapterView.OnItemClickListener {
private View mMenuView;
private ListView mListView;
private SelectDepartmentAdapter mAdapter = null;
private List mDataList = new ArrayList();
private String brandName = "";
private TextView mTvName;
private SelectDepartOnclick s;
public void setData( List mDataList,String brandName){
this.mDataList=mDataList;
this.brandName=brandName;
mAdapter.setList(mDataList);
mTvName.setText(brandName);
}
public SelectDepatmentPopupView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.view_choosedepatment, null);
initData(context);
initView();
initEvent();
// 设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(UIUtils.getHeight()/2);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.PopupAnimation);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
setBackgroundDrawable(dw);
mMenuView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
}
private void initData(Context cx) {
mAdapter = new SelectDepartmentAdapter(cx, mDataList);
}
public void setSelectDepartmentOnclickLister(SelectDepartOnclick s){
this.s=s;
}
private void initView() {
mListView = (ListView) mMenuView.findViewById(R.id.depart_listview);
mListView.setAdapter(mAdapter);
mTvName = (TextView) mMenuView.findViewById(R.id.depart_tv_name);
}
private void initEvent() {
mListView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
if(s!=null)
s.selectDepart(mAdapter.getList().get(i).fldSerial,mAdapter.getList().get(i).fld_modelid);
this.dismiss();
}
public interface SelectDepartOnclick{
void selectDepart(String a,int id);
}
}
//bean类
public class SelectDepartmentBean {
public String fldSerial;//车辆品牌系列名称
public int fld_modelid;//车辆系列id
public String firstSpell;//车辆首字母拼音
}
//初始化编辑框
mPopView = new SelectDepatmentPopupView(getActivity());
model = (EditText) view.findViewById(R.id.model);
//车系popwindow 选择
mPopView.setSelectDepartmentOnclickLister(new SelectDepatmentPopupView.SelectDepartOnclick() {
@Override
public void selectDepart(String a, int id) {
car.setText(a);
modleId = id;//车系ID
modleName = a;//车系名称
model.setText("");//车型
}
});
//数据进行解析和显示到编辑框
List mDataList = new ArrayList();
private void getData(final Context cx, final String mBrandName) {
String brandName = "";
try {
brandName = URLEncoder.encode(mBrandName, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//请求车系数据
AutoboleRequest request = new AutoboleRequest();
final String str = GloableParams.MODLEBNAME_KET + "?modelName=" + brandName;//MODLEBNAME_KET为url地址
request.reuqstData(cx, str, null, GloableParams.SESSIONID, new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (msg.obj != null) {
mDataList = new Gson().fromJson((String) msg.obj, new TypeToken>() {
}.getType());
Collections.sort(mDataList, new PinyinDepartmentComparator());//拼音排序
mPopView.setData(mDataList, mBrandName);
mPopView.showAtLocation(mGo_select_model, Gravity.BOTTOM, 0, 0);
} else {
ToastUtil.showErrorToast(cx, "获取车辆系别失败");
}
break;
case 2:
ToastUtil.showErrorToast(cx, "获取车辆系别失败");
break;
}
super.handleMessage(msg);
}
});
}