部分索引得到部分位置返回0 [英] section indexer get section for position returns 0

查看:195
本文介绍了部分索引得到部分位置返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我有一个扩展类 ArrayAdapter<字符串> 和农具 SectionIndexer 。当实现方法 getPositionForSection getSectionForPosition 我已经发现了一些奇怪的行为。

In my project I have class that extends ArrayAdapter<String> and implements SectionIndexer. When implementing methods getPositionForSection and getSectionForPosition I have found some strange behaviour.

为什么部分索引器工作正常时, getSectionForPosition 返回0?

Why section indexer works properly when getSectionForPosition returns 0?

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

这实现被用在许多教程,例如:

this implementation is used in many tutorials, for example:

http://androidopentutorials.com/android-listview-fastscroll/

http://www.survivingwithandroid.com/2012/12/android-listview-sectionindexer-fastscroll.html

文件说,

鉴于适配器内的位置,则返回的索引   部分对象的数组中相应的部分。

Given a position within the adapter, returns the index of the corresponding section within the array of section objects.

所以,如果我的名单上有5个项目开始以字母A和一些项目开始以字母B,然后getSectionForPosition(5)应返回1。

so if my list have 5 items starting with letter "A" and some items starting with letter "B", then getSectionForPosition(5) should return 1.

推荐答案

虽然的 sectionIndexer (拉动滚动条滑块时滚动浏览章节索引)的基本功能是不受影响,返回一个恒定的方法 getSectionForPosition 可以通过列表刷卡(当导致滚动条定位错误行为,例如滚动条移动窗外在y轴)。

While the basic functionality of the sectionIndexer (scrolling through the section indices when pulling the scrollbar thumb) is unaffected, returning a constant in the method getSectionForPosition can lead to a misbehaviour of the scrollbar positioning when swiping through the list (e.g. the scrollbar moving out of the window on the y-axis).

显然,这个行为不端仅当列表或单段超过一定的长度,所以对于较短列表索引似乎正常工作(但事实上没有)发生。我在上面的方法返回一个常量几次,当经历过这种玩忽职守大名单,并以正确的方式实施 getSectionForPosition 固定它。

Apparently this misbehaviour only occurs when the list or single sections exceed a certain length, so for shorter lists the indexer might seem to work correctly (but in fact doesn't). I experienced this misbehaviour in larger lists when returning a constant in the above method several times and fixed it by implementing getSectionForPosition in a correct way.

不过,因为这可能是有趣的人,我可以提供一个方法的样本实现。让 azIndexer 的HashMap 包含正确部分 - &GT;我的指数位置制图和时listItems 为您安排适配器的项目。下面建立了一个位置 - &GT; sectionIndex 映射存储在 positionIndexer

However, since it might be interesting for others I can provide a sample implementation of that method. Let azIndexer be a HashMap containing a correct section -> position mapping of my indices and listItems be the items arranged by the adapter. The following builds a position -> sectionIndex mapping stored in positionIndexer.

List<Integer> values = new ArrayList();
for (int i : azIndexer.values()) {
    values.add(i);
}
values.add(listItems.size()-1);
Collections.sort(values);

int k = 0;
int z = 0;
for(int i = 0; i < values.size()-1; i++) {
    int temp = values.get(i+1);
    do {
        positionIndexer.put(k, z);
        k++;
    } while(k < temp);
    z++;
}

这将随后在 getSectionForPosition 方法可以使用​​:

which will then be used in the getSectionForPosition method:

@Override
public int getSectionForPosition(int position) {
    return positionIndexer.get(position);
}

这篇关于部分索引得到部分位置返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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