2012-07-24 18 views
7

我創建了ListView,它使用的是FastScroll。 (參見圖)當用戶點擊任何按鈕下的(即所有曲目,藝術家,專輯),每次下面的自定義ArrayAdater被稱爲Android:FastScrolling SectionIndexer getSections()僅被調用一次

ArrayAdapter<String> adapter = new ScrollIndexListAdapter(Listing.this, elements); 
//Code for ScrollIndexListAdapter is below 

和相同的ListView更新。

enter image description here

問題:根據我的Android調查,getSections()方法被調用一次(即只有當第一時間ScrollIndexListAdapter被調用)。

這一次的部分填充& fastScrolling完美的作品。

但是當我點擊藝術家/專輯更新ListView時,getSections()方法沒有被調用。所以使用了較舊的部分,FastScrolling仍然顯示舊字母的預覽。

那麼,如何在每次更新ListView時更新部分?

有一個setSections()方法,但我無法找到如何使用它。

代碼ScrollIndexListAdapter類:

public class ScrollIndexListAdapter extends ArrayAdapter implements 
     SectionIndexer { 

    // Variables for SectionIndexer List Fast Scrolling 
    HashMap<String, Integer> alphaIndexer; 
    String[] sections; 
    private static ArrayList<String> list = new ArrayList<String>(); 

    public ScrollIndexListAdapter(Context context, ArrayList<String> list) { 
     super(context, android.R.layout.simple_list_item_1, android.R.id.text1, 
       list); 
     this.list.clear(); 
     this.list.addAll(list); 
     /* 
     * Setting SectionIndexer 
     */ 
     alphaIndexer = new HashMap<String, Integer>(); 
     int size = list.size(); 
     for (int x = 0; x < size; x++) { 
      String s = (String) list.get(x); 
      // Get the first character of the track 
      String ch = s.substring(0, 1); 
      // convert to uppercase otherwise lowercase a -z will be sorted 
      // after upper A-Z 
      ch = ch.toUpperCase(); 
      if (!alphaIndexer.containsKey(ch)) { 
       alphaIndexer.put(ch, x); 
      } 
     } 
     Set<String> sectionLetters = alphaIndexer.keySet(); 
     // create a list from the set to sort 
     ArrayList<String> sectionList = new ArrayList<String>(
       sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     sectionList.toArray(sections); 
    } 

    /* 
    * Methods for AphhabelIndexer for List Fast Scrolling 
    */ 
    @Override 
    public int getPositionForSection(int section) { 
     String letter = (String) sections[section]; 
     return alphaIndexer.get(letter); 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     String letter = (String) sections[position]; 
     return alphaIndexer.get(letter); 
    } 

    @Override 
    public Object[] getSections() { 
     return sections; 
    } 
} 
+0

我在Android上的經驗有限,但我認爲應該通過在適配器中調用'notifyDataSetChanged()'來廣播底層數據源的更改。 – 2014-05-14 12:17:18

回答

2

看來,最新的FastScroll版本不存在這樣的問題。但是,就你而言,有一個轉折。將適配器設置爲ListView時,禁用並啓用快速滾動。請參閱下面的代碼:

  adapter = new ScrollIndexListAdapter(this, data); 
      listView.setFastScrollEnabled(false); 
      listView.setAdapter(adapter); 
      listView.setFastScrollEnabled(true); 
+0

我發現這個錯誤,對我來說這工作正常:) – JMR 2015-10-26 16:28:49

相關問題