給大家分享一個很實用的城市選擇列表抓歼,城市列表估計大家都很熟悉讥此,在此記錄一下
效果圖
城市數(shù)據(jù)在assets下的China_cities.db文件
一拢锹、控件的測量
public class SideLetterBar extends View {
private static final String[] b = {"定位", "熱門", "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 choose = -1;
private Paint paint = new Paint();
private boolean showBg = false;
private OnLetterChangedListener onLetterChangedListener;
private TextView overlay;
public SideLetterBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SideLetterBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SideLetterBar(Context context) {
super(context);
}
/**
* 設(shè)置懸浮的textview
* @param overlay
*/
public void setOverlay(TextView overlay){
this.overlay = overlay;
}
@SuppressWarnings("deprecation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (showBg) {
canvas.drawColor(Color.TRANSPARENT);
}
int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for (int i = 0; i < b.length; i++) {
paint.setTextSize(getResources().getDimension(R.dimen.side_letter_bar_letter_size));
paint.setColor(getResources().getColor(R.color.cp_gray));
paint.setAntiAlias(true);
if (i == choose) {
paint.setColor(getResources().getColor(R.color.cp_gray_deep));
// paint.setFakeBoldText(true); //加粗
}
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnLetterChangedListener listener = onLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBg = true;
if (oldChoose != c && listener != null) {
if (c >= 0 && c < b.length) {
listener.onLetterChanged(b[c]);
choose = c;
invalidate();
if (overlay != null){
overlay.setVisibility(VISIBLE);
overlay.setText(b[c]);
}
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null) {
if (c >= 0 && c < b.length) {
listener.onLetterChanged(b[c]);
choose = c;
invalidate();
if (overlay != null){
overlay.setVisibility(VISIBLE);
overlay.setText(b[c]);
}
}
}
break;
case MotionEvent.ACTION_UP:
showBg = false;
choose = -1;
invalidate();
if (overlay != null){
overlay.setVisibility(GONE);
}
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnLetterChangedListener(OnLetterChangedListener onLetterChangedListener) {
this.onLetterChangedListener = onLetterChangedListener;
}
public interface OnLetterChangedListener {
void onLetterChanged(String letter);
}
}
二、實現(xiàn)類的關(guān)鍵部分
public CityListAdapter(Context mContext, List<City> mCities) {
this.mContext = mContext;
this.mCities = mCities;
this.inflater = LayoutInflater.from(mContext);
if (mCities == null){
mCities = new ArrayList<>();
}
mCities.add(0, new City("定位", "0"));
mCities.add(1, new City("熱門", "1"));
int size = mCities.size();
letterIndexes = new HashMap<>();
sections = new String[size];
for (int index = 0; index < size; index++){
//當前城市拼音首字母
String currentLetter = PinyinUtils.getFirstLetter(mCities.get(index).getPinyin());
//上個首字母萄喳,如果不存在設(shè)為""
String previousLetter = index >= 1 ? PinyinUtils.getFirstLetter(mCities.get(index - 1).getPinyin()) : "";
if (!TextUtils.equals(currentLetter, previousLetter)){
letterIndexes.put(currentLetter, index);
sections[index] = currentLetter;
}
}
}
最后給出demo的地址卒稳,歡迎大家訪問和使用。
https://gitee.com/oubajunping/chengshixuanzeliebiao