16

ListViewその実装用のアダプターを使用していますSectionIndexer。xml ファイルで true に設定されてListViewいます。fastScrollEnabledAndroid 2.2 および 2.3 ではすべてがうまく機能しますが、Android 3.0 を搭載したタブレットでアプリケーションをテストすると、いくつかのセクションでスクロールバーが消えます。たとえば、リストを下にスクロールすると、AB の文字で始まる要素でスクロールバーが表示されますが、CH の文字では表示されず、H の後に再び表示されます。

このアダプターは、ListView でコンテンツをアルファベット順に並べ替えて、fastscroll を使用できるようにするために作成されています。

アプリケーションは 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;
    }
}

charListは、最後に表示されたインデックスで文字を格納する HashMap であるため、文字「A」で始まる 6 つの要素がある場合、キー「A」の値は 5 などになります。

alphabetすべての既存の最初の文字を含む String 配列です。

4

1 に答える 1