这是我的问题的短视频。列表的最后一部分滚动得非常快,正如您可以从滚动条中看到的那样。这是我的代码(相关部分):
public TotemAdapter (Activity activity, List<Totem> list) {
_activity = activity;
totemList = list;
showDelete = false;
var items = list.ToArray ();
alphaIndex = new Dictionary<string, int>();
for (int i = 0; i < items.Length; i++) {
var key = items[i].title[0].ToString();
if (!alphaIndex.ContainsKey(key))
alphaIndex.Add(key, i);
}
sections = new string[alphaIndex.Keys.Count];
alphaIndex.Keys.CopyTo(sections, 0);
sectionsObjects = new Java.Lang.Object[sections.Length];
for (int i = 0; i < sections.Length; i++) {
sectionsObjects[i] = new Java.Lang.String(sections[i]);
}
}
public int GetPositionForSection (int section) {
return alphaIndex[sections[section]];
}
public int GetSectionForPosition(int position) {
return 1;
}
public Java.Lang.Object[] GetSections () {
return sectionsObjects;
}
有人知道我该怎么解决吗?
提前谢谢。
我通过在GetSectionForPosition
中返回0而不是1来修复它。
您可以使用SetFriction方法来控制列表视图的快速滚动速度。请参阅Android Listview中的一个示例,降低的滚动速度
在getSectionForPosition
中返回常量可能会导致在列表中滑动时滚动条定位不当。虽然你的问题看起来已经解决了,但事实并非如此。getSectionForPosition
方法的文档中写道:
给定适配器中的位置,返回节对象数组中相应节的索引。
因此,当您在列表中滑动时,您当前查看的部分最终会发生更改(例如,您正在访问联系人中的"B"部分)。当您在列表中滑动时,此方法告诉滚动条必须将其自身定位在何处(这与getPositionForSection
方法相对应,当您使用滚动条在列表中滚动时,该方法会跳到列表中的部分)。
我在这里提供了getSectionForPosition
方法的示例实现