Android Quick Index (city list and contacts)

Source: Internet
Author: User


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.