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. Group the list (with the same characteristics) and be able to quickly navigate to the first item of the group2. The 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
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
@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
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 = MS Ectionindexter.getstartpositionofsection (string.valueof (Words[index)); if (position = =-1) { return true; } Mlistview.setselection (position); } return true; }
as follows:
Source: Https://github.com/TUBB/CityIndex
Android Quick Index (city list and contacts)