获取当前的信件?

时间:2014-03-13 22:33:52

标签: 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);
    }
}

3 个答案:

答案 0 :(得分:0)

让您的自定义SectionIndexer覆盖以下内容:

@Override
public int getPositionForSection(int section)
{
    int result = super.getPositionForSection(section);
    Log.i("TAG", "getPositionForSection " + result);
    return result;
}

当用户快速滚动列表时,将调用此方法,您应该能够使用结果从字母数组中获取实际字母。

答案 1 :(得分:0)

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);
    }

答案 2 :(得分:0)

我发布了一个工作项目on github with alphabetIndexer和部分标题。即便这样也可以通过手机接听联系人

您可以使用listner根据滚动查找当前的alpgabet。看看这个activity.

private OnScrollListener mOnScrollListener = new 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;
        }

      }

   };

注意:

  • 上面的代码可能有拼写错误。
  • 您可以随意在设备或模拟器中使用联系人运行项目,并相应地进行更改。