Code example for SectionIndexer

Methods: getPositionForSection, getSectionForPosition, getSections

0
    public int getPositionForSection(int sectionIndex) {
        if (mIndexer == null) {
            return -1; 
        } 
 
        return mIndexer.getPositionForSection(sectionIndex);
    } 
 
    /** 
     * @param position relative position in the indexed partition 
     */ 
    public int getSectionForPosition(int position) {
        if (mIndexer == null) {
            return -1; 
        } 
 
        return mIndexer.getSectionForPosition(position);
    } 
 
    @Override 
    public int getPinnedHeaderCount() { 
        if (isSectionHeaderDisplayEnabled()) { 
            return super.getPinnedHeaderCount() + 1; 
        } else { 
            return super.getPinnedHeaderCount(); 
        } 
    } 
 
    @Override 
    public View getPinnedHeaderView(int viewIndex, View convertView, ViewGroup parent) {
        if (isSectionHeaderDisplayEnabled() && viewIndex == getPinnedHeaderCount() - 1) {
            if (mHeader == null) {
                mHeader = createPinnedSectionHeaderView(mContext, parent);
            } 
            return mHeader;
        } else { 
            return super.getPinnedHeaderView(viewIndex, convertView, parent);
        } 
    } 
 
    @Override 
    public void configurePinnedHeaders(PinnedHeaderListView listView) {
        super.configurePinnedHeaders(listView);
 
        if (!isSectionHeaderDisplayEnabled()) { 
            return; 
        } 
 
        int index = getPinnedHeaderCount() - 1;
        if (mIndexer == null || getCount() == 0) {
            listView.setHeaderInvisible(index, false);
        } else { 
            int listPosition = listView.getPositionAt(listView.getTotalTopPinnedHeaderHeight());
            int position = listPosition - listView.getHeaderViewsCount();
 
            int section = -1;
            int partition = getPartitionForPosition(position);
            if (partition == mIndexedPartition) {
                int offset = getOffsetInPartition(position);
                if (offset != -1) {
                    section = getSectionForPosition(offset);
                } 
            } 
 
            if (section == -1) {
                listView.setHeaderInvisible(index, false);
            } else { 
                setPinnedSectionTitle(mHeader, (String)mIndexer.getSections()[section]);
                if (section == 0) {
                    setPinnedHeaderContactsCount(mHeader);
                } else { 
                    clearPinnedHeaderContactsCount(mHeader);
                }