新项目添加到ListView后,有什么方法可以重新索引SectionIndexer吗?

我找到了这个solution,但是覆盖层位于刷新SectionIndexer之后的左上角。

有人有想法么?

最佳答案

一旦FastScroller(它是AbsListView扩展自ListView的类)在通过调用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";
        }
    }
    

    关于android - 重新索引/刷新SectionIndexer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3898749/

    10-10 20:09