标签:android
最近需要实现一个城市列表的快速索引功能。类似于联系人应用,根据姓名首字母快速索引功能。
列表‘特征’和分组首项进行关联
for (int i = 0; i < mCitys.size(); i++) { City city = mCitys.get(i); String cityId = city.getId(); if(cityId == null || "".equals(cityId)) continue; String section = cityId.toUpperCase().substring(0, 1); if(!indexMap.containsKey(section)){ indexMap.put(section, i); } }
@Override protected void onDraw(Canvas canvas) { heightCenter = getMeasuredHeight()/2 - preHeight*WORDS.length/2; for (int i = 0; i < WORDS.length; i++) { canvas.drawText(String.valueOf(WORDS[i]), getMeasuredWidth()/2, preHeight + (i * preHeight) + heightCenter, mPaint); } super.onDraw(canvas); }
public boolean onTouchEvent(MotionEvent event) { int startY = (int) event.getY() - heightCenter; int index = startY / preHeight; if (index >= WORDS.length) { index = WORDS.length - 1; } else if (index < 0) { index = 0; } if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { int position = mSectionIndexter.getStartPositionOfSection(String.valueOf(WORDS[index])); if (position == -1) { return true; } mListView.setSelection(position); } return true; }
源码:https://github.com/TUBB/CityIndex
标签:android
原文地址:http://blog.csdn.net/tu_bingbing/article/details/42151909