android - 在 Scroll 上获取当前字母?

标签 android scroll accessibility android-cursoradapter

我正在构建一个辅助功能应用程序,并希望在用户滚动时大声朗读 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/

相关文章:

ios - Swift:Voice Over 从 View 过渡到警报 View 或另一个 View

android - 滚动距离 - 无障碍服务 ACTION_SCROLL_FORWARD?

accessibility - iOS 辅助功能 - 切换控制

android - 如何在 onCreate() 方法中显示 MainActivity 的布局?

javascript - 响应式下拉汉堡菜单

android - 动态按钮文本?

iphone - UITableViewCell 内的水平 UIScrollView

javascript - 如何将焦点设置在我的网页的一部分然后向下滚动

android - 如何在 Android 中安装 Charles Proxy ssl 证书

android - 在 android v2 应用程序中显示谷歌地图在打开 Activity 时崩溃,膨胀异常二进制文件编号 #2