2012-06-11 49 views
16

我使用的适配器ListView实现了SectionIndexerListView已将fastScrollEnabled设置为xml文件中的true。在Android 2.2和2.3上一切正常,但当我使用Android 3.0在平板电脑上测试我的应用程序时,某些部分的滚动条消失了。例如,当我向下滚动列表时,以字母开头的元素A-B滚动条是可见的,但对于字母C-H,它不是,然后H再次可见。在HoneyComb的特定部分使用SectionIndexer时,滚动条消失

此适配器用于在ListView中按字母顺序排序内容,以便可以使用快速滚动。

应用程序是为API级别8设计的,因此我无法使用fastScrollAlwaysVisible。

这里是我的适配器代码:

public class AlphabetSimpleAdapter extends SimpleAdapter implements SectionIndexer { 

    private HashMap<String, Integer> charList; 
    private String[] alphabet; 
    public Typeface tfSansMedium; 
    public Context mContext; 
    public int mResource; 
    public int[] mTo; 
    public List<? extends Map<String, ?>> mData; 
    public String mTitleKey; 

    public AlphabetSimpleAdapter(Context context, 
      List<? extends Map<String, ?>> data, int resource, String[] from, 
      int[] to, String titleKey /* key sent in hashmap */) { 
     super(context, data, resource, from, to); 
     mData = data; 
     mTitleKey = titleKey; 
     mContext = context; 
     mResource = resource; 
     mTo = new int[to.length]; 
     for (int i = 0; i < to.length; i ++) 
     { 
      mTo[i] = to[i]; 
     } 
     charList = new HashMap<String, Integer>(); 
     int size = data.size(); 
     tfSansMedium = Typeface.createFromAsset(context.getAssets(), "fonts/VitesseSans-Medium.otf"); 
     for(int i = 0; i < size; i++) { 
         // Parsing first letter of hashmap element 
      String ch = data.get(i).get(titleKey).toString().substring(0, 1); 
      ch = ch.toUpperCase(); 

      if(!charList.containsKey(ch)) { 
       charList.put(ch, i); // Using hashmap to avoid duplicates 
      } 
     } 

     Set<String> sectionLetters = charList.keySet(); // A set of all first letters 

     ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); // Creating arraylist to be able to sort elements 
     Collections.sort(sectionList, Collator.getInstance(new Locale("pl", "PL"))); // Sorting elements 
     alphabet = new String[sectionList.size()]; 
     sectionList.toArray(alphabet); 

    } 

    // Methods required by SectionIndexer 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = li.inflate(mResource, null); 
     } 
     for (int i = 0; i < mTo.length; i ++) { 
      TextView tv = (TextView)v.findViewById(mTo[i]); 
      if (tv != null) tv.setTypeface(tfSansMedium); 
     } 
     return super.getView(position, v, parent); 
    } 

    @Override 
    public int getPositionForSection(int section) { 
     if(!(section > alphabet.length-1)) { 
      return charList.get(alphabet[section]); 
     } 
     else { 
      return charList.get(alphabet[alphabet.length-1]); 
     } 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     return charList.get(mData.get(position).get(mTitleKey).toString().substring(0, 1)); 
    } 

    @Override 
    public Object[] getSections() { 
     return alphabet; 
    } 
} 

是我存储与他们的最后出现的索引字母一个HashMap,所以当我开始以字母“A” 6元,价值为关键“ A“是5等等。

alphabet是一个字符串数组,包含所有现有的第一个字母。

+0

你知道吗?我正在处理同样的问题。它狡猾我的问题是与getSectionForPosition方法。 – KickingLettuce

+0

不幸的不是。花了很多时间弄清楚这一点,但最后不得不离开它。 –

+0

此问题已在此处提及:http://stackoverflow.com/a/13470842/1140682 – saschoar

回答

2

我正面临同样的问题。我只是把我的最小SDK版本设为8,将traget设为16,并编写了下面的代码。一切正常。

   int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
       if (currentapiVersion <= android.os.Build.VERSION_CODES.FROYO){ 
        // Do something for froyo and above versions 
        fblist.setFastScrollEnabled(true); 


       } else if(currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB){ 
        // do something for phones running an SDK before froyo 
        fblist.setFastScrollEnabled(true); 
        fblist.setFastScrollAlwaysVisible(true); 
       } 
+0

它的工作原理,谢谢! – Cabezas