我使用AlphabetIndexer,SectionIndexer,Custom CursorAdapter作为自定义ListView。以下是相同的代码。但是在滚动列表时我无法看到字母表。我还尝试了以下链接how to combine both DISPLAY_NAME and NUMBER in a customized CursorAdapter?
public class ContactListCustomCursorAdapter extends CursorAdapter implements
SectionIndexer {
private LayoutInflater inflater;
private CursorsForContacts cursorsForContacts;
private AlphabetIndexer alphabetIndexer;
public ContactListCustomCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
inflater = LayoutInflater.from(context);
cursorsForContacts = new CursorsForContacts(context);
alphabetIndexer = new AlphabetIndexer(
cursor,
cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME),
" ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//alphabetIndexer.setCursor(cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String contactId = setContactName(view, cursor);
setContactPhoneNumbers(view, contactId);
setContactEmails(view, contactId);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = inflater.inflate(R.layout.custom_contact_list_item,
parent, false);
return view;
}
private String setContactName(View view, Cursor cursor) {
int username = cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
TextView userName = (TextView) view.findViewById(R.id.userName);
userName.setText(cursor.getString(username));
return cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
}
private void setContactPhoneNumbers(View view, String contactId) {
Cursor phoneCursor = cursorsForContacts.getPhoneNumberCursor(contactId);
TextView phoneNumber = (TextView) view.findViewById(R.id.userNumber);
while (phoneCursor.moveToNext()) {
phoneNumber
.setText(phoneCursor.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
}
private void setContactEmails(View view, String contactId) {
Cursor emailCursor = cursorsForContacts.getEmailCursor(contactId);
TextView userEmailId = (TextView) view.findViewById(R.id.userEmailId);
while (emailCursor.moveToNext()) {
userEmailId
.setText(emailCursor.getString(emailCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
}
@Override
public int getPositionForSection(int section) {
return alphabetIndexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
return alphabetIndexer.getSectionForPosition(position);
}
@Override
public Object[] getSections() {
return alphabetIndexer.getSections();
}
}
我错过了什么吗?
答案 0 :(得分:0)
您需要将android:fastScrollEnabled="true"
添加到布局xml中的ListView。
答案 1 :(得分:0)
对于OP来说可能为时已晚,但其他在这里遇到同样问题的人可能会受益......
在我的情况下,字母索引器是"随机"在某些情况下,在其他情况下没有在同一个屏幕上。
如果列表不是很长,则快速滚动,因此字母索引器将被暂停。确切的行为可以在FastScroller
类中找到,它是AbsListView
的帮助类。那里有一段代码决定"列表是否很长"
final boolean longList = childCount > 0 && itemCount / childCount >= MIN_PAGES;
MIN_PAGES
定义为值为4.如果您的列表项计数不是至少4倍,则子数(可见行)快速滚动,因此不会出现字母索引器。