【发布时间】:2013-12-08 00:21:26
【问题描述】:
我写了一个应用程序,它的布局中包含一个支持快速滚动的 ListView(即,拖动滚动条滑块可以让您快速向上或向下滚动列表)。这在所有版本的 Jelly Bean (4.1-4.3) 中都能完美运行。但是,在我更新到 Kit Kat 后,快速滚动不再起作用,我的滚动条只是一个普通的滚动条。但是,如果我退出我的应用程序并重新进入,则会出现快速滚动。如何快速滚动以在 Kit Kat 中始终如一地工作?我已经复制并粘贴了下面的列表视图适配器代码:
// Adds section popup for fast scroll
class NameListAdapter extends SimpleAdapter implements SectionIndexer {
HashMap<String, Integer> alphaIndexer;
private String[] sections;
private ArrayList<String> sectionList;
private List<HashMap<String, String>> data = null;
public NameListAdapter (Context context, List<HashMap<String, String>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
alphaIndexer = new HashMap<String, Integer>();
sectionList = new ArrayList<String>();
this.data = data;
int size = data.size();
for (int i = 0; i < size; i++) {
HashMap<String, String> myData = (HashMap <String, String>) data.get(i);
// Get first character
String ch = myData.get("name").substring(0, 1);
// Convert character to upper case
ch = ch.toUpperCase();
// Put first char/index into our HashMap
if (!alphaIndexer.containsKey(ch)) {
alphaIndexer.put(ch, i);
sectionList.add(ch);
}
}
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
@Override
public int getPositionForSection(int section) {
if (section >= sections.length) {
return getCount() - 1;
}
return alphaIndexer.get(sections[section]);
}
@Override
public int getSectionForPosition(int pos) {
if (pos > data.size()) {
return 0;
}
HashMap<String, String> myData = (HashMap <String, String>) data.get(pos);
String ch = myData.get("name").substring(0, 1);
// Convert character to upper case
ch = ch.toUpperCase();
for (int i = 0; i < sectionList.size(); i++) {
if (sectionList.get(i).equals(ch)) {
return i;
}
}
return 0;
}
@Override
public Object[] getSections() {
return sections;
}
}
【问题讨论】:
-
我打开了一个关于这个的错误,所以请随意给它加星标。 code.google.com/p/android/issues/…
-
这似乎在 Android v4.4.3 中得到解决。
标签: android android-listview android-4.4-kitkat