StackOverflow社区,
我目前正在使用androidlistviewssections的功能,但在android 4.4api中似乎有一个问题。
我有一个cursoradapter,它从sqlite数据库填充项。创建适配器时,它会分割其项。
用户有可能将ListView项的排序从a-z更改为z-a。当他这样做时,将创建并设置一个新的CursorAdapter,当然这将再次索引他的项。
我使用以下代码设置了新的适配器,它似乎强制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在快速滚动时显示旧的部分:因此,当用户将排序更改为z-a时,listview在快速滚动时仍按a-z顺序显示该部分覆盖。
我希望你知道我的意思。
这是一个已知的错误,是否有解决方法?
非常感谢。
编辑:这是我使用的适配器的代码。它基本上基于两个游标。第一个保存要显示的数据,第二个保存索引。
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);
}

最佳答案

到目前为止,我找到的唯一“解决方法”是完全删除旧的listview并在布局中添加一个新的listview。因此,在像在我的初始文章中那样设置适配器之前,您可以检查用户是否运行kitkat,然后将新的listview添加到布局中。

listView.setFastScrollEnabled(false);
if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT) {
    swapListView();
}
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);

swapListView()方法很简单:
private void swapListView() {
    //save layout params
    ViewGroup.LayoutParams listViewParams = null;
    if (listView != null) {
        listViewParams = listView.getLayoutParams();
    } else {
        listViewParams = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    //frame is a FrameLayout around the ListView
    frame.removeView(listView);

    listView = new ListView(this);
    listView.setLayoutParams(listViewParams);
    //other ListView initialization code like divider settings

    frame.addView(listView);
}

这会迫使索引/节在设置适配器后刷新,但在我看来这是一段脏代码,不太适合作为解决方案。

关于android - Android刷新ListView部分-覆盖在4.4中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20730301/

10-12 04:02
查看更多