16

我正在使用适配器来ListView实现SectionIndexerListViewfastScrollEnabled在 xml 文件中设置为 true。在 Android 2.2 和 2.3 上一切正常,但是当我在装有 Android 3.0 的平板电脑上测试我的应用程序时,滚动条在某些部分消失了。例如,当我向下滚动列表时,在以字母 AB 开头的元素处滚动条可见,但对于字母 CH 则不可见,然后在 H 之后再次可见。

此适配器用于在 ListView 中按字母顺序对内容进行排序,以便可以使用 fastscroll。

应用程序是为 API Level 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;
    }
}

charList是一个 HashMap,我在其中存储字母及其最后出现的索引,因此当我有 6 个以字母“A”开头的元素时,键“A”的值为 5,依此类推。

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

4

1 回答 1

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);
                 }
于 2013-04-12T11:23:09.790 回答