部分索引器获取位置的部分返回0 [英] section indexer get section for position returns 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
->我的索引和 listItems
的位置position ->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屋!