【问题标题】:AlphabetIndexer not working with Custom ListViewAlphabetIndexer 不适用于自定义 ListView
【发布时间】:2011-11-09 10:21:48
【问题描述】:

我将 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();
}

}

我错过了什么吗?

【问题讨论】:

标签: android listview indexing customization


【解决方案1】:

您需要将android:fastScrollEnabled="true" 添加到布局xml 中的ListView。

【讨论】:

    【解决方案2】:

    对于 OP 来说可能为时已晚,但在这里遇到同样问题的其他人可能会受益......

    在我的情况下,字母索引器在某些情况下是“随机”存在的,而在其他情况下不在同一屏幕上。

    如果列表不是很长,快速滚动和字母索引器将被暂停。确切的行为可以在FastScroller 类中找到,它是AbsListView 的帮助类。那里有一段代码决定“列表是否很长”

    final boolean longList = childCount > 0 && itemCount / childCount >= MIN_PAGES;
    

    MIN_PAGES 的值定义为 4。如果您的列表项计数不是子计数(可见行)快速滚动条的至少 4 倍,那么您就有了它,因此字母索引器将不会出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2010-11-28
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多