我正在构建一个可访问性应用程序,希望在用户滚动时大声读出AlphabetIndexer上的当前字母。
如何运行一些代码onScroll?我如何在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);
}
}发布于 2014-03-21 01:51:34
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);
}发布于 2014-03-17 19:44:26
让您的自定义SectionIndexer覆盖以下内容:
@Override
public int getPositionForSection(int section)
{
int result = super.getPositionForSection(section);
Log.i("TAG", "getPositionForSection " + result);
return result;
}这将在用户快速滚动列表时调用,并且您应该能够使用结果从字母表数组中获取实际字母。
发布于 2014-03-22 08:23:00
我已经张贴了一个工作项目基于alphabetIndexer的github研究和节标题。即使这样也会从你的手机上取得联系。
您可以使用listner基于滚动找到当前的alpgabet。看看这个活动。
私有OnScrollListener mOnScrollListener =新的OnScrollListener() {
private int lastFirstVisibleItem = -1;
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
// firstVisibleItem corresponding to the index of AlphabetIndexer(eg, B-->Alphabet index is 2)
int sectionIndex = mIndexer.getSectionForPosition(firstVisibleItem);
//next section Index corresponding to the positon in the listview
int nextSectionPosition = mIndexer.getPositionForSection(sectionIndex + 1);
if(firstVisibleItem != lastFirstVisibleItem) {
if(String.valueOf(alphabet.charAt(sectionIndex)).equalsIgnoreCase("a")){
//Play the sound for A.
}
lastFirstVisibleItem = firstVisibleItem;
}
}
};注:
https://stackoverflow.com/questions/22392417
复制相似问题