我正在尝试使用 AlphabetIndexer 实现快速滚动条,但是当光标发生变化时,它不会刷新索引缓存。在我的 CursorAdapter 构造函数中,我调用了 setCursor(cursor)
但没有任何变化,并且根据文档:
Your adapter is responsible for updating the cursor by calling setCursor(Cursor) if the cursor changes. getPositionForSection(int) method does the binary search for the starting index of a given section (alphabet).
但是什么也没有发生。我将其用于搜索过滤器,因此当我搜索联系人时,它会使用联系人更新列表,因此 AlphabetIndexer 应该用于更新列表中新项目的索引。
示例:我的整个列表以“A”开头的联系人开头,以“E”开头的联系人结尾。所以 AlphabetIndexer 会在它的缓存中有这个索引。
但是,让我们尝试搜索以“C”开头的联系人,假设我有 250 个以“C”开头的联系人。因此,我必须快速滚动浏览此联系人,并且必须显示“C”索引,但它不只显示“C”,而是显示所有索引,这些索引是在我拥有整个列表时显示的。
这是我的 CursorAdapter 构造函数,我在其中为我键入的每个字母调用 setCursor(cursor):
public MyCursorAdapter(Context context, Cursor cursor, ArrayList<Integer> ids)
{
super(context, cursor);
try
{
mAlphaIndexer = new AlphabetIndexer(cursor, cursor.getColumnIndexOrThrow("Name")," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
notifyDataSetChanged();
mAlphaIndexer.setCursor(cursor);
this.mSelectedContacts = ids;
Log.e("MyCursorAdapter", "construtor: ");
}
catch(Exception ex)
{
Log.e("MyCursorAdapter", "Error: " + ex);
}
finally
{
mAlphaIndexer.setCursor(cursor);
}
}
最佳答案
我通过在设置适配器后按顺序调用它解决了这个问题:
listView.setFastScrollEnabled(false);
listView.setFastScrollEnabled(true);
这对我有用。
关于android - AlphabetIndexer setCursor 不更新其缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6711178/