7

ListViewを使用している を作成しましたFastScroll。(写真を参照)ユーザーが下のボタン(つまり、すべてのトラック、アーティスト、アルバム)のいずれかをクリックすると、次のカスタムArrayAdaterが呼び出されるたびに

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

同じ ListView が更新されます。

ここに画像の説明を入力

問題: Android での調査によると、このgetSections()メソッドは 1 回だけ呼び出されます (つまり、ScrollIndexListAdapter が初めて呼び出されたときだけ)。

今回はセクションが作成され、fastScrolling が完全に機能します。

しかし、Artists/Album をクリックして 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;
    }
}
4

1 に答える 1

3

最新の FastScroll バージョンにはその問題はないようです。しかし、あなたの場合、方向転換があります。アダプターを ListView に設定するときは、高速スクロールを無効にしてから有効にします。以下のコードを参照してください。

            adapter = new ScrollIndexListAdapter(this, data);
            listView.setFastScrollEnabled(false);
            listView.setAdapter(adapter);
            listView.setFastScrollEnabled(true);
于 2013-10-17T19:19:51.817 に答える