我想我会在这里发帖,希望不会太晚。
注意:这不是使用 AlphabetIndexer,我不认为使用三个集合来管理列表是一个好主意,虽然它很简单,并且解释了这个概念。
以下是如何使用回调的基本示例:
public LinkedHashMap<Integer,String> sectionList = new LinkedHashMap<Integer,String>();
public HashMap<Integer,Integer> sectionPositions = new HashMap<Integer, Integer>();
public HashMap<Integer,Integer> positionsForSection = new HashMap<Integer, Integer>();
当你有你的“位置”数组(预购),这将创建三个哈希图来跟踪事物,非常简单的实现,非常容易阅读:
if( locations != null && locations.size() > 0 ) {
//Iterate through the contacts, take the first letter, uppercase it, and use that as a key to reference the alphabetised list constructed above.
for( int i = 0; i < locations.size(); i++ ) {
String startchar =locations.get(i).getStartCharacterForAlphabet();
if( startchar != null ) {
if( sectionList.containsValue(startchar) == false ) {
sectionList.put(Integer.valueOf(i),startchar);
positionsForSection.put(Integer.valueOf(sectionList.size() - 1), Integer.valueOf(i));
}
}
sectionPositions.put(Integer.valueOf(i), sectionList.size() - 1);
}
}
下面是三个回调:
@Override
public int getPositionForSection(int section) {
return positionsForSection.get(Integer.valueOf(section)).intValue();
}
@Override
public MyLocation getItem(int position) {
if( locations.size() > position ) {
return locations.get(position);
}
return null;
}
@Override
public int getSectionForPosition(int position) {
return sectionPositions.get(Integer.valueOf(position)).intValue();
}
希望对你有帮助!