A quick indexing function for a city list has recently been required. Similar to the Contact app, the Quick Index feature is based on the first letter of the name.
There are only two issues that need to be resolved to implement this feature:
1, the list is grouped (with the same characteristics), and can quickly navigate to the group's first item 2, right grouping ' feature ' Fast index bar implementation
The first is a good solution, and list items can be grouped according to the same ' characteristics ', such as city lists that can be grouped according to the city name of the same initial letter. How to locate the first item in a group, simply associate the ' feature ' of the group with the first subscript of the grouping, and quickly index the column to locate the first item in the group quickly.
The second problem can be implemented by custom controls, which can be implemented in many forms, by drawing a picture of a whole grouping ' feature ' (hard to expand), or by dynamically plotting each grouping ' feature ' (extensibility)
Here are some of the key implementation of the code, basically is the embodiment of the above thought, if you have special needs, a little change can be done
List ' feature ' and group first item are associated
?
123456789 |
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);
}
}
|
Drawing of Fast indexes
?
123456789 |
@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);
}
|
User interaction, quickly navigate to index entries
?
1234567891011121314151617 |
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
;
}
|
As follows:
A companion tour, a free dating site: www.jieberu.com
Push family, free tickets, scenic spots: www.tuituizu.com
Android Quick Index (city list and contacts)