嗨 Stackoverflow 社区,
我目前正在使用 Android ListViewsections 功能,但 Android 4.4 API 似乎存在问题。我有一个从 SQLite 数据库填充项目的 CursorAdapter。当适配器被创建时,它会分割他的项目。用户可以将 ListView 项目的排序从 AZ 更改为 ZA。当他这样做时,将创建并设置一个新的 CursorAdapter,这当然会再次索引他的项目。我使用以下代码设置新的 Adapter,这似乎强制 Listview 调用 getSections() 并在快速滚动时刷新部分:
//A new instance of adapter is created here with the new cursor, see code at bottom
listView.setFastScrollEnabled(false);
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);
上面的代码在 Android 4.3 之前运行良好,但在 4.4 中停止工作。在 4.4 中,ListView 在快速滚动时显示旧部分:因此,当用户将排序更改为 ZA 时,ListView 在快速滚动时仍然显示此部分-覆盖顺序为 AZ。我希望你知道我的意思。
这是一个已知的错误,是否有解决方法?
非常感谢。
编辑:这是我使用的适配器的代码。它基本上基于2个游标。第一个保存要显示的数据,第二个保存索引。
public class SectionCheckableCursorAdapter extends CursorAdapter implements SectionIndexer {
private LayoutInflater mInflater;
private Map<Integer, Object> mSectionIndices = new TreeMap<Integer, Object>();
private Object[] mSections;
private Integer[] mSectionPositions;
public SectionCheckableCursorAdapter(Context context, Cursor c, Cursor indexCursor) {
super(context, c, false);
mInflater = LayoutInflater.from(context);
buildIndex(indexCursor);
}
private void buildIndex(Cursor indexCursor) {
//init
mSectionIndices = new TreeMap<Integer, Object>();
List<Integer> sectionPositionsList = new ArrayList<Integer>();
//get column indices from cursor
int indexFirstLetter = indexCursor.getColumnIndex("firstLetter");
int indexCount = indexCursor.getColumnIndex("count");
//build sections
int currentCount = 0;
while (indexCursor.moveToNext()) {
final int count = indexCursor.getInt(indexCount);
String firstChar = indexCursor.getString(indexFirstLetter);
if (firstChar == null || firstChar.isEmpty()) {
firstChar = " ";
}
if (StringUtils.isNumber(firstChar)) {
firstChar = "#";
}
if (!mSectionIndices.containsValue(firstChar)) {
mSectionIndices.put(currentCount, firstChar);
sectionPositionsList.add(currentCount);
}
currentCount += count;
}
mSectionPositions = new Integer[sectionPositionsList.size()];
sectionPositionsList.toArray(mSectionPositions);
Collection<Object> sections_collection = mSectionIndices.values();
mSections = new Object[sections_collection.size()];
sections_collection.toArray(mSections);
}
@Override
public int getPositionForSection(int section) {
if (section > mSectionPositions.length - 1) {
if (getCursor() != null)
return getCursor().getCount() - 1;
return 0;
} else {
return mSectionPositions[section];
}
}
@Override
public int getSectionForPosition(int position) {
for (int i = 0; i < mSectionPositions.length-1; i++) {
if (position >= mSectionPositions[i] && position < mSectionPositions[i+1]) {
return i;
}
}
return mSectionPositions.length-1;
}
@Override
public Object[] getSections() {
return mSections;
}
}
这是在设置适配器之前创建游标和适配器的方式,如顶部代码中所示:
//Query for "real" data and the indices. The indices are created with a query because
//this is much faster than iterating through all entries in the "data" cursor
mCursor = mDatabase.rawQuery(query, null);
mIndexCursor = mDatabase.rawQuery(indexQuery, null);
if (mCursor != null) {
adapter = new SectionCheckableCursorAdapter(getSherlockActivity(), mCursor, mIndexCursor);
}