在实现SectionIndexer
的ArrayAdapter
中,有一个代码可以检查以相同的第一个字母开头的列表项,因此可以对其进行合并。
像这样:
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);
我的问题是,以这种方式合并是否会影响快速滚动?有时在使用SectionIndexer
的ListViews上,快速滚动并不总是平滑的,而是"波涛汹涌"的。我可以将SectionIndexer
从这种情况中删除,快速滚动会突然平滑而成比例地滚动。
添加代码:
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
return sections;
}
SectionIndexer确实会影响快速滚动。
使用SectionIndexer时,您的意思是希望用户能够精确地跳转到数据集的这些部分。如果这些部分中的数据分布不均匀,那么快速滚动条将与它在部分集中的进度成比例移动,而不是与它在数据集中每个单独项目中的进度成正比。
这是故意的;通过这种方式,当用户拖动快速滚动拇指时,每个部分都被赋予相同的权重。精确地瞄准任何一个部分就像瞄准任何其他部分一样容易,即使一个部分只有一个项目,而它两侧的部分有数百个。