我正在构建一个辅助功能应用程序,并希望在用户滚动时大声朗读 AlphabetIndexer 上的当前字母。
如何在 Scroll 上运行一些代码?我如何在 CursorAdapter 中获取当前字母?
谢谢
这就是我正在尝试的(我试图只留下相关的部分):
private class ContactsAdapter extends CursorAdapter implements SectionIndexer {
private LayoutInflater mInflater;
private AlphabetIndexer mAlphabetIndexer;
public ContactsAdapter(Context context) {
super(context, null, 0);
mInflater = LayoutInflater.from(context);
final String alphabet = context.getString(R.string.alphabet);
mAlphabetIndexer = new AlphabetIndexer(null, ContactsQuery.SORT_KEY, alphabet);
}
// ...
@Override
public Object[] getSections() {
return mAlphabetIndexer.getSections();
}
@Override
public int getPositionForSection(int i) {
if (getCursor() == null) {
return 0;
}
Log.i("TAG", "getPositionForSection " + mAlphabetIndexer.getPositionForSection(i));
return mAlphabetIndexer.getPositionForSection(i);
}
@Override
public int getSectionForPosition(int i) {
if (getCursor() == null) {
return 0;
}
return mAlphabetIndexer.getSectionForPosition(i);
}
}
最佳答案
SectionIndexer.getSectionForPosition
将返回 SectionIndexer.getSections
中相应部分的索引。因此,要检索某个部分的当前字母,您需要做的就是调用:
@Override
public int getSectionForPosition(int i) {
if (getCursor() == null) {
return 0;
}
// Prints the letter for the current section
System.out.println(getSections()[mAlphabetIndexer.getSectionForPosition(i)]);
return mAlphabetIndexer.getSectionForPosition(i);
}
关于android - 在 Scroll 上获取当前字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392417/