SectionIndexer with GridView in Android

半腔热情 提交于 2019-12-21 19:47:48

问题


Is it possible to use a SectionIndexer with a GridView in Android? Fast scroll is working fine, and I'm using a custom adapter that extends BaseAdapter. The adapter is currently implementing SectionIndexer and seems to be identical to the examples shown online and on Stack Overflow. This made me think if it's even possible to do with a GridView and a custom adapter.


回答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>

That's a basic example, but there's not much more to it than that. Implementing SectionIndexer is pretty simple.



来源:https://stackoverflow.com/questions/10389531/sectionindexer-with-gridview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!