Example usage for android.widget SectionIndexer getSectionForPosition

List of usage examples for android.widget SectionIndexer getSectionForPosition

Introduction

In this page you can find the example usage for android.widget SectionIndexer getSectionForPosition.

Prototype

int getSectionForPosition(int position);

Source Link

Document

Given a position within the adapter, returns the index of the corresponding section within the array of section objects.

Usage

From source file:com.dm.xz.views.PinnedSectionListView.java

int findCurrentSectionPosition(int fromPosition) {
    ListAdapter adapter = getAdapter();/*from   w w  w.j a v a  2s .c  o m*/

    if (fromPosition >= adapter.getCount())
        return -1; // dataset has changed, no candidate

    if (adapter instanceof SectionIndexer) {
        // try fast way by asking section indexer
        SectionIndexer indexer = (SectionIndexer) adapter;
        int sectionPosition = indexer.getSectionForPosition(fromPosition);
        int itemPosition = indexer.getPositionForSection(sectionPosition);
        int typeView = adapter.getItemViewType(itemPosition);
        if (isItemViewTypePinned(adapter, typeView)) {
            return itemPosition;
        } // else, no luck
    }

    // try slow way by looking through to the next section item above
    for (int position = fromPosition; position >= 0; position--) {
        int viewType = adapter.getItemViewType(position);
        if (isItemViewTypePinned(adapter, viewType))
            return position;
    }
    return -1; // no candidate found
}

From source file:com.yahala.ui.Views.PinnedSectionListView.java

int findCurrentSectionPosition(int fromPosition) {
    ListAdapter adapter = getAdapter();/*from w  w  w. j  a  v a 2s. c  o  m*/

    if (adapter instanceof SectionIndexer) {
        // try fast way by asking section indexer
        SectionIndexer indexer = (SectionIndexer) adapter;
        int sectionPosition = indexer.getSectionForPosition(fromPosition);
        int itemPosition = indexer.getPositionForSection(sectionPosition);
        int typeView = adapter.getItemViewType(itemPosition);
        if (isItemViewTypePinned(adapter, typeView)) {
            return itemPosition;
        } // else, no luck
    }

    // try slow way by looking through to the next section item above
    for (int position = fromPosition; position >= 0; position--) {
        int viewType = adapter.getItemViewType(position);
        if (isItemViewTypePinned(adapter, viewType))
            return position;
    }
    return -1; // no candidate found
}