【问题标题】:SectionIndexer with GridView in Android在 Android 中使用 GridView 的 SectionIndexer
【发布时间】:2012-04-30 19:37:59
【问题描述】:

是否可以在 Android 中使用 SectionIndexerGridView?快速滚动工作正常,我正在使用扩展 BaseAdapter 的自定义适配器。该适配器当前正在实现SectionIndexer,并且似乎与在线和 Stack Overflow 上显示的示例相同。这让我想到是否可以使用 GridView 和自定义适配器。

【问题讨论】:

  • 当然可以,只要您使用Cursor 对数据进行排序。
  • 我没有使用光标。我已经将它存储在一个 ArrayList 中,但我想我可以将它更改为光标。有使用光标的示例吗?
  • @MichellBak 你能告诉我你在gridview中的sectionindexer示例吗?

标签: android gridview android-3.0-honeycomb adapter android-4.0-ice-cream-sandwich


【解决方案1】:
static class YOUR_ADAPTER extends SimpleCursorAdapter implements SectionIndexer {

private AlphabetIndexer mIndexer;

 YOUR_ADAPTER (Context context, AlbumBrowserActivity currentactivity,
            int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to);

        getColumnIndices(cursor);
    } 

    private void getColumnIndices(Cursor cursor) {
        if (cursor != null) {
            YOUR_COLUMN = cursor.getColumnIndexOrThrow(WHAT_YOU'RE_SORTING);

            if (mIndexer != null) {
                mIndexer.setCursor(cursor);
            } else {
                mIndexer = new AlphabetIndexer(cursor, YOUR_COLUMN, mResources.getString(
                        R.string.fast_scroll_alphabet));
            }
        }
    }

    @Override
    public Object[] getSections() {
        return mIndexer.getSections();
    }

    @Override   
    public int getPositionForSection(int section) {
        return mIndexer.getPositionForSection(section);
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }
}

fast_scroll_alphabet String

<string name="fast_scroll_alphabet">\u0020ABCDEFGHIJKLMNOPQRSTUVWXYZ</string>

这是一个基本示例,但仅此而已。实现SectionIndexer 非常简单。

【讨论】:

  • 我错过了 AlphabetIndexer :-)
猜你喜欢
  • 2011-10-15
  • 1970-01-01
  • 2011-10-16
  • 2015-01-08
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
相关资源
最近更新 更多