Create easy alphabetical scrolling in ListView?

后端 未结 6 1630
無奈伤痛
無奈伤痛 2020-12-05 03:09

I\'m looking to emulate the functionality in the latest Music app, namely the nice little cursor that pops up which allows one to scroll super fast to the artist/album/track

6条回答
  •  伪装坚强ぢ
    2020-12-05 03:59

    Here is the subclass of ArrayAdapter I'm using. Note that the objects I pass in has already been sorted alphabetically, with Collections.sort(objects).

    class SectionIndexingArrayAdapter extends ArrayAdapter implements
            SectionIndexer {
    
        HashMap sectionsMap = new HashMap();
        ArrayList sectionsList = new ArrayList();
    
        ArrayList sectionForPosition = new ArrayList();
        ArrayList positionForSection = new ArrayList();
    
        public SectionIndexingArrayAdapter(Context context, int textViewResourceId,
                List objects) {
            super(context, textViewResourceId, objects);
    
            // Note that List objects has already been sorted alphabetically
            // e.g. with Collections.sort(objects) **before** being passed to
            // this constructor.
    
            // Figure out what the sections should be (one section per unique
            // initial letter, to accommodate letters that might be missing,
            // or characters like ,)
            for (int i = 0; i < objects.size(); i++) {
                String objectString = objects.get(i).toString();
                if (objectString.length() > 0) {
                    String firstLetter = objectString.substring(0, 1).toUpperCase();
                    if (!sectionsMap.containsKey(firstLetter)) {
                        sectionsMap.put(firstLetter, sectionsMap.size());
                        sectionsList.add(firstLetter);
                    }
                }
            }
    
            // Calculate the section for each position in the list.
            for (int i = 0; i < objects.size(); i++) {
                String objectString = objects.get(i).toString();
                if (objectString.length() > 0) {
                    String firstLetter = objectString.substring(0, 1).toUpperCase();
                    if (sectionsMap.containsKey(firstLetter)) {
                        sectionForPosition.add(sectionsMap.get(firstLetter));
                    } else
                        sectionForPosition.add(0);
                } else
                    sectionForPosition.add(0);
            }
    
            // Calculate the first position where each section begins.
            for (int i = 0; i < sectionsMap.size(); i++)
                positionForSection.add(0);
            for (int i = 0; i < sectionsMap.size(); i++) {
                for (int j = 0; j < objects.size(); j++) {
                    Integer section = sectionForPosition.get(j);
                    if (section == i) {
                        positionForSection.set(i, j);
                        break;
                    }
                }
            }
        }
    
        // The interface methods.
    
        public int getPositionForSection(int section) {
            return positionForSection.get(section);
        }
    
        public int getSectionForPosition(int position) {
            return sectionForPosition.get(position);
        }
    
        public Object[] getSections() {
            return sectionsList.toArray();
        }
    }
    

提交回复
热议问题