首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用实现SectionIndexer的自定义ListAdapter,FastScroll的行为很奇怪

使用实现SectionIndexer的自定义ListAdapter,FastScroll的行为很奇怪
EN

Stack Overflow用户
提问于 2011-07-20 00:00:13
回答 2查看 2.5K关注 0票数 1
代码语言:javascript
复制
class MySimpleAdapter extends SimpleAdapter implements SectionIndexer {

    HashMap<String, Integer> letters;
    Object[] sections;
    AlphabetIndexer alphaIndexer;
    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to, 
            HashMap<String, Integer> letters, Object[] sections) {
        super(context, data, resource, from, to);

        this.letters = letters;
        this.sections = sections;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.common_image_row1, null);
        }

        HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
        Integer idArtist = Integer.parseInt((String) data.get("idArtist"));

        convertView.setTag(idArtist);

        ((TextView) convertView.findViewById(R.id.item_name))
            .setText((String) data.get("sName"));

        ImageView image = (ImageView) convertView.findViewById(R.id.item_image);
        image.setTag((String) data.get("sImageUrl"));           
        image.setImageResource(R.drawable.default_artwork);

        ((TextView) convertView.findViewById(R.id.item_count))
            .setText((String) data.get("iSongs") + " Songs");

        if (!bScrolling) {
            new API.DownloadImagesTask().execute(image);
        }
        return convertView;
    }

    public int getPositionForSection(int section) {
        String letter = (String) sections[section];

        return letters.get(letter);

    }

    public int getSectionForPosition(int position) {
        return 0;
    }

    public Object[] getSections() {
        return sections;
    }
}

该活动在一个单独的AysncTask中接收一个JSON对象。这个对象由各种JSONArrays组成,它们的键是项的第一个字母,也就是数组的项。因此,该数组由一堆以字母"B“开头的项组成。因此,JSONArray的键是"B“。

代码语言:javascript
复制
{
    B: ["ball", "buck", "bill"]
    C: ["charlie", "chuck", "chap"]
}

对象不一定包含字母表中的所有字母。我也意识到JSONObjects不能保证顺序,所以我对它们进行排序。

代码语言:javascript
复制
 List> maps = new ArrayList>();             ArrayList sections = new ArrayList();             HashMap letters = new HashMap();
代码语言:javascript
复制
        String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"
                ,"O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        int k = 0;
        for (int i = 0; i < alphabet.length; i++) {
            if (playlistArtists.optJSONArray(alphabet[i]) != null) {
                try {
                    JSONArray artists = playlistArtists.getJSONArray(alphabet[i]);
                    sections.add(alphabet[i]);

                    for (int j = 0; j < artists.length(); j++) {
                        JSONObject artist = (JSONObject) artists.get(j);
                        HashMap<String, String> map= new HashMap<String, String>();
                        map.put("idArtist", artist.getString("idArtist"));
                        map.put("sName", artist.getString("sName"));
                        map.put("sImageUrl", artist.getString("sImageUrl-thumb"));
                        map.put("iSongs", artist.getString("iSongs"));
                        maps.add(map);

                        letters.put(alphabet[i], k);
                        k++;
                    }
                } catch (JSONException e) {
                    Log.d(TAG,"JSONException in Music::GetMusicCatlog.doInBackground");
                    e.printStackTrace();
                }

            }
        }
        SimpleAdapter adapter = new MySimpleAdapter(Catalog.this, 
                maps,
                R.layout.common_image_row1,
                new String[] {"idArtist","sName", "sImageUrl", "iSongs" },
                new int[] {R.id.item_id, R.id.item_name, R.id.item_image, R.id.item_count }, 
                letters, 
                sections.toArray());
        setListAdapter(adapter);

我遇到的问题是FastScroll。大部分情况下,我已经让一切正常工作了。该列表按第一个字母分组,当使用FastScroll时,该字母会出现在弹出窗口中并转到正确的组中。问题是,当我在转到所需部分后释放FastScroll时,FastScroll会“跳”到列表的随机部分。它不会停留在我放手的地方。我认为它在这一节中有一个武断的地方,因为我没有正确地实现SectionIndexer。我认为问题出在这种方法上。

代码语言:javascript
复制
public int getSectionForPosition(int position) {
        return 0;
    }

我只是不确定如何正确地实现SectionIndexer方法……

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-20 05:50:26

我曾多次看到getSectionForPosition以与getPositionForSection相同的方式实现

代码语言:javascript
复制
    public int getPositionForSection(int section) {
        String letter = (String) sections[section];

        return letters.get(letter);
    }

    public int getSectionForPosition(int position) {
        String letter = (String) sections[position];

        return letters.get(letter);
    }
票数 -2
EN

Stack Overflow用户

发布于 2015-03-31 20:07:45

DR的第一个答案不可能是正确的。如果需要getSectionForPosition方法的示例实现,请参见here

您说得对,这看起来像是您的getSectionForPosition实现出了问题。到目前为止提供的答案不可能是正确的,因为您需要返回某个位置相关的部分的索引,而不是一个部分相关的位置(这就是您的getPositionForSection方法应该做的)。

因此,您应该(再次)修改您的getSectionForPosition方法。您需要将每个位置有效地映射到一个区段。文档中写道:

在给定适配器内的位置的情况下,

将返回section对象数组中相应section的索引。

如果您需要有关如何构建此类映射的示例,请参阅here。如果正确实现,它将解决跳转滚动条的问题,并正确定位它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6750248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档