安卓:FastScrolling SectionIndexer getSections()被调用一次 [英] Android: FastScrolling SectionIndexer getSections() is called only once

查看:180
本文介绍了安卓:FastScrolling SectionIndexer getSections()被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个的ListView 这是使用 FastScroll 。 (参见图)当用户点击任何按钮下的(即所有曲目,艺术家,专辑),每次下面的自定义ArrayAdater叫

  ArrayAdapter<字符串>适配器=新ScrollIndexListAdapter(Listing.this,元素);
// $ C $下ScrollIndexListAdapter低于
 

和相同的ListView被更新。

问题:根据我的Andr​​oid调查, getSections()方法被调用一次(即只有当第一时间ScrollIndexListAdapter被调用)。

这一次的部分填充和放大器;在fastScrolling完美的作品。

但是,当我点击艺术家/专辑的 getSections更新的ListView()方法不叫。所以较早部分被使用,且FastScrolling仍显示旧字母表previews

那么,如何才能让部分得到更新时,每次在ListView更新?

有一个 setSections()的方法,但我无法找到如何使用它。

$ C $下ScrollIndexListAdapter类:

 公共类ScrollIndexListAdapter扩展ArrayAdapter工具
        SectionIndexer {

    //为SectionIndexer列表快速滚动变量
    HashMap的<字符串,整数> alphaIndexer;
    的String []段;
    私有静态的ArrayList<字符串>名单=新的ArrayList<字符串>();

    公共ScrollIndexListAdapter(上下文的背景下,ArrayList的<字符串>列表){
        超(背景下,android.R.layout.simple_list_item_1,android.R.id.text1,
                清单);
        this.list.clear();
        this.list.addAll(名单);
        / *
         *环境SectionIndexer
         * /
        alphaIndexer =新的HashMap<字符串,整数>();
        INT大小=则为list.size();
        为(中间体X = 0 X  - 其中;大小; X ++){
            字符串s =(字符串)list.get(X);
            //获取轨道的第一字符
            串CH = s.substring(0,1);
            //转换为大写,否则小写-z进行排序
            //后上A-Z
            CH = ch.toUpperCase();
            如果(!alphaIndexer.containsKey(CH)){
                alphaIndexer.put(CH,X);
            }
        }
        设置<字符串> sectionLetters = alphaIndexer.keySet();
        //创建一个列表从集合进行排序
        ArrayList的<字符串> sectionList =新的ArrayList<字符串>(
                sectionLetters);
        Collections.sort(sectionList);
        部分=新的String [sectionList.size()];
        sectionList.toArray(段);
    }

    / *
     *方法AphhabelIndexer的列表快速滚动
     * /
    @覆盖
    公众诠释getPositionForSection(INT部分){
        串字母=(字符串)部分[第]
        返回alphaIndexer.get(函);
    }

    @覆盖
    公众诠释getSectionForPosition(INT位置){
        串字母=(字符串)第[位置]
        返回alphaIndexer.get(函);
    }

    @覆盖
    公共对象[] getSections(){
        返回段;
    }
}
 

解决方案

看来,最新的FastScroll版本不存在这样的问题。但是,你的情况,有一个转身。当设置适配器到ListView,禁用和启用快速滚动。请参见下面的code:

 适配器=新ScrollIndexListAdapter(这个数据);
            listView.setFastScrollEnabled(假);
            listView.setAdapter(适配器);
            listView.setFastScrollEnabled(真正的);
 

I've created a ListView which is using FastScroll. (see pic) When the user clicks any of the below Button (viz. All Tracks, Artists, Album), everytime the following custom ArrayAdater is called

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

and the same ListView is updated.

PROBLEM: According to my investigation in Android, the getSections() method is called only once (i.e. only when the first time ScrollIndexListAdapter is called).

This time the sections are populated & the fastScrolling works perfectly.

But when I update the ListView by clicking on Artists/Album, the getSections() method is not called. So the older sections are used, and the FastScrolling still shows previews of old alphabets.

So, how can I make sections get updated everytime when the ListView is updated?

There is a setSections() method, but I'm not able to find how to use it.

Code for ScrollIndexListAdapter class:

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;
    }
}

解决方案

It seems that latest FastScroll version doesn't have that problem. But, in your case, there's a turn around. When setting the adapter to the ListView, disable and enable the fast scrolling. See the code below:

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

这篇关于安卓:FastScrolling SectionIndexer getSections()被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆