2010-10-10 82 views
15

有什麼辦法可以在新項目添加到ListView後重新索引SectionIndexer?重新索引/刷新SectionIndexer

我發現這個solution,但覆蓋是在SectionIndexer刷新後左上角的位置。

任何人有任何想法?

+0

重裝部分列表的ListView我在做類似的東西這裏http://stackoverflow.com/questions/10224233/alphabetindexer-with-custom-adapter-managed-by-loadermanager – toobsco42 2012-04-25 06:51:24

回答

4

一旦FastScroller(其AbsListViewListView從擴展)通過調用SectionIndexer#getSections()獲得您的部分,它永遠不會,除非你啓用/禁用快速滾動像你提到的鏈接中提到重新獲得它們。要獲取要顯示在屏幕上的值,FastScroller會調用該部分的toString方法。

一個潛在的解決方案是有一個自定義SectionIndexer具有以下特徵:

  • 切片陣列是固定長度(段的預期數量的最大長度。例如,如果所述部分代表英語字母表這將是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"; 
    } 
} 
1

我發現,要做到這一點的最好辦法是打電話setContentView(R.layout.whatever),然後用新的適配器/新數據項重新填充ListView。這將用您的新項目重新繪製ListView,並且FastScroll疊加層將出現在正確的位置。

0

我發現notifyDataSetInvalidated做工精細,這裏的想法:

public class MyAdapter extends XXXAdapter implements SectionIndexer { 
    ... 
    public void updateDataAndIndex(List data, Map index) { 
     // update sections 
     // update date set 
     notifyDataSetInvalidated(); 
    } 
} 

更新您的數據集和索引(部分)不知何故,然後notifyDataSetInvalidated,該指數將刷新。

0

您可以強制通過listView.setAdapter(yourAdapter)