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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。