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, 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 @Overrideprotected 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)

Related Article

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.