我想在这里发布一下,希望还不算太晚。
注意:这并不是使用 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 ) {
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();
}
希望对您有所帮助!