public void setAdapter(Adapter adapter) { if (adapter instanceof SectionIndexer) { mIndexer = (SectionIndexer) adapter; mSections = (String[]) mIndexer.getSections(); } else if (adapter instanceof HeaderViewListAdapter) { hasHead = true; mIndexer = (SectionIndexer) (((HeaderViewListAdapter) adapter).getWrappedAdapter()); mSections = (String[]) mIndexer.getSections(); } }
public Object[] getSections() { if (mIndexer == null) { return new String[] {" "}; } else { return mIndexer.getSections(); } }
/** @param position relative position in the indexed partition */ public int getSectionForPosition(int position) { if (mIndexer == null) { return -1; } return mIndexer.getSectionForPosition(position); }
/** @return relative position of the section in the indexed partition */ public int getPositionForSection(int sectionIndex) { if (mIndexer == null) { return -1; } return mIndexer.getPositionForSection(sectionIndex); }
public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // If down event occurs inside index bar region, start indexing if (mState != STATE_HIDDEN && contains(ev.getX(), ev.getY())) { setState(STATE_SHOWN); // It demonstrates that the motion event started from index bar mIsIndexing = true; // Determine which section the point is in, and move the list to that section mCurrentSection = getSectionByPoint(ev.getY()); int offset = 0; if (hasHead) { offset = 1; } mListView.setSelection(mIndexer.getPositionForSection(mCurrentSection) + offset); return true; } break; case MotionEvent.ACTION_MOVE: if (mIsIndexing) { // If this event moves inside index bar if (contains(ev.getX(), ev.getY())) { // Determine which section the point is in, and move the list to that section mCurrentSection = getSectionByPoint(ev.getY()); int offset = 0; if (hasHead) { offset = 1; } mListView.setSelection(mIndexer.getPositionForSection(mCurrentSection) + offset); } return true; } break; case MotionEvent.ACTION_UP: if (mIsIndexing) { mIsIndexing = false; mCurrentSection = -1; } if (mState == STATE_SHOWN) setState(STATE_HIDING); break; } return false; }
public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // 按下,开始索引 // If down event occurs inside index bar region, start indexing if (mState != STATE_HIDDEN && contains(ev.getX(), ev.getY())) { // 设置索引条状态:已显示 setState(STATE_SHOWN); // It demonstrates that the motion event started from index bar mIsIndexing = true; // Determine which section the point is in, and move the list to // that section // 根据纵坐标点获取当前字母位置 mCurrentSection = getSectionByPoint(ev.getY()); // ListView定位到指定Item mListView.setSelection(mIndexer.getPositionForSection(mCurrentSection)); return true; } break; case MotionEvent.ACTION_MOVE: // 移动 // 正在索引 if (mIsIndexing) { // If this event moves inside index bar if (contains(ev.getX(), ev.getY())) { // Determine which section the point is in, and move the // list to that section mCurrentSection = getSectionByPoint(ev.getY()); mListView.setSelection(mIndexer.getPositionForSection(mCurrentSection)); } return true; } break; case MotionEvent.ACTION_UP: // 抬起 // 没有在索引 if (mIsIndexing) { mIsIndexing = false; mCurrentSection = -1; } if (mState == STATE_SHOWN) setState(STATE_HIDING); break; } return false; }
int findCurrentSectionPosition(int fromPosition) { ListAdapter adapter = getAdapter(); 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 }
@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); } // Compute the item position where the current partition begins int partitionStart = getPositionForPartition(mIndexedPartition); if (hasHeader(mIndexedPartition)) { partitionStart++; } // Compute the item position where the next section begins int nextSectionPosition = partitionStart + getPositionForSection(section + 1); boolean isLastInSection = position == nextSectionPosition - 1; listView.setFadingHeader(index, listPosition, isLastInSection); } } }
public void setAdapter(Adapter adapter) { if (adapter instanceof SectionIndexer) { mIndexer = (SectionIndexer) adapter; mSections = (String[]) mIndexer.getSections(); } }