重新索引/刷新SectionIndexer

ton*_*nyc 15 android listview

将新项目添加到ListView后,有没有办法重新索引SectionIndexer?

我找到了这个解决方案,但是在刷新SectionIndexer之后,叠加层位于左上角.

有人有主意吗?

Thi*_*ira 4

一旦FastScroller(它的扩展AbsListViewListView)通过调用获取您的部分SectionIndexer#getSections(),它就永远不会重新获取它们,除非您像您提到的链接中提到的那样启用/禁用快速滚动。为了获取要在屏幕上显示的值,FastScroller 调用该部分的 toString 方法。

一种可能的解决方案是定制SectionIndexer具有以下特征的定制:

  • sections 数组是固定长度的(预期节数的最大长度。例如,如果节代表英文字母,则为 26)
  • 使用自定义对象来表示部分,而不是使用字符串
  • 覆盖toString自定义部分对象的方法,以根据当前的“部分值”显示您想要的内容。
  • -

例如在您的自定义SectionIndexer中

private int mLastPosition;

public int getPositionForSection(int sectionIndex) {
    if (sectionIndex < 0) sectionIndex = 0;
    // myCurrentSectionLength is the number of sections you want to have after 
    // re-indexing the items in your ListView
    // NOTE: myCurrentSectionLength must be less than getSections().length
    if (sectionIndex >= myCurrentSectionLength) sectionIndex = myCurrentSectionLength - 1;
    int position = 0;
    // --- your logic to find the position goes in here
    // --- e.g. see the AlphabeticIndexer source in Android repo for an example

    mLastPosition = position;
    return mLastPosition;
} 

public Object[] getSections() {
    // Assume you only have at most 3 section for this example
    return new MySection[]{new MySection(), new MySection(), new MySection()};
}

// inner class within your CustomSectionIndexer
public class MySection {
    MySection() {}

    public String toString() {
        // Get the value to displayed based on mLastPosition and the list item within that position
        return "some value";
    }
}
Run Code Online (Sandbox Code Playgroud)