Inside ArrayAdapter implementing SectionIndexer there is code that checks for list items that starts with the same first letter — so it can be consolidated.
Like this:
alphaIndexer = new HashMap<String, Integer>();
int size = objects.length;
for (int i = 0; i < size; i++) {
// Log.d("ObjectLength", String.valueOf(objects.length));
ItemObject it = objects[i];
String name = it.name;
String s = name.substring(0, 1);
s = s.toUpperCase();
if (!alphaIndexer.containsKey(s)) {
alphaIndexer.put(s, i);
}
}
Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
// sectionList.toArray(sections);
for (int i = 0; i < sectionList.size(); i++)
sections[i] = sectionList.get(i);
My question, does consolidating this way effect FastScrolling? Sometimes on ListViews using SectionIndexer, the Fast Scroll isn’t always smooth but “choppy”. I can remove the SectionIndexer from the situation and Fast Scroll suddenly scrolls smoothly and proportionally.
ADDED CODE:
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
return sections;
}
SectionIndexer does indeed affect fast scrolling.
When using a SectionIndexer, you’re saying that you want users to be able to precisely jump to those sections of your data set. If data in those sections is distributed unevenly, then the fast scroller will move proportional to its progress through the set of sections instead of proportional to its progress through each individual item in the data set.
This is intentional; it’s done this way so that when a user is dragging the fast scroll thumb each section is given equal weight. Precisely targeting any section is as easy as targeting any other section, even if one section only has one item and the sections on either side of it have hundreds.