2012-08-26 140 views
1

Inside ArrayAdapter Implementing SectionIndexer有代码检查以相同的第一个字母开头的列表项目 - 因此可以合并。SectionIndexer如何影响Android快速滚动?

像这样:

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); 

我的问题,并巩固这种方式的效果FastScrolling?有时在使用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; 
    } 
+0

您发布的代码在每次调用getSectionForPosition' /'getPositionForSection'时执行? – zapl

+0

@zapl我只是将这些方法添加到顶部,以便您可以看到我在做什么。 – KickingLettuce

+0

http://stackoverflow.com/questions/11988886/fastscrollbar-go-out-of-screen-when-sectionindexer-实施 – GOLDEE

回答

5

SectionIndexer确实影响快速滚动。

使用SectionIndexer时,您说的是您希望用户能够精确跳转到数据集的这些部分。如果这些部分中的数据分布不均匀,那么快速滚动器将根据其在部分集合中的进度成比例地移动,而不是与数据集中每个单独项目的进度成比例。

这是故意的;它以这种方式完成,以便当用户拖动快速滚动拇指时,每个部分被赋予相同的权重。精确定位任何部分与定位任何其他部分一样简单,即使一个部分只有一个项目,而其任一侧的部分都有数百个项目。

+0

感谢您的意见。还有一个问题需要澄清:当我说这是“波涛汹涌”时,我应该明确指出,当您经常滚动时,快速滚动“拇指滚动”符号与屏幕高度不成正比。有时它跳到底部,其他时间,它滚动屏幕。但是,如果您手动抓住滚动条,它就会成比例。在我的应用程序中,我保持快速滚动,并删除了SectionIndexer,并完美滚动(无论哪种方式)。我说的话有道理吗? – KickingLettuce

+5

它在屏幕外移动不正常。 :)确保你提供了SectionIndexer接口的所有方法 - 它需要能够从节索引到适配器位置以及从适配器位置到该项目的节索引这两种方式进行映射。 **编辑**只是看到你添加的代码;你的'getSectionForPosition'方法报告每个项目都属于第一部分。它应该报告位置所属部分的索引。 – adamp

+0

该方法的正确return语句是什么?它实际上是“1”,之后我改变为“0”而没有改变。 (编辑:文档说它返回“部分索引,如果位置超出范围,则部分索引必须被裁剪以落在部分数组的大小范围内。” - 你知道这是如何实现的吗?) – KickingLettuce