Android quick index (City list and contacts)

Source: Internet
Author: User

Android quick index (City list and contacts)

 

You need to quickly index a city list. Similar to a contact application, the quick indexing function is based on the first letter of the name.

To implement this function, you only need to solve two problems:
1. Grouping the list (having the same feature) and quickly locating the first item in the group. 2. Grouping the 'feature' on the right to quickly index the column.
The first problem is better solved. The list items can be grouped based on the same 'feature'. For example, the city list can be grouped based on the city names with the same initials. To locate the first item of a group, you only need to associate the 'feature' of the Group with the subscript of the first item of the Group to quickly locate the first item of the group.
The second problem can be achieved through custom controls. There are many implementation forms. You can draw a whole group of 'feature' images (difficult to expand ), you can also dynamically draw the 'feature' of each group (highly scalable)
The following are some key code for implementation, which is basically the embodiment of the above idea. If you have special requirements, you can simply make some changes.

The list 'feature' is associated with the first item of the group.

 

                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);            }        }

 

 

Quick Indexing
    @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 to quickly locate index items
    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:


 

 

 

 

 

 



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.