/** @return relative position of the section in the indexed partition */
  public int getPositionForSection(int sectionIndex) {
    if (mIndexer == null) {
      return -1;
    }

    return mIndexer.getPositionForSection(sectionIndex);
  }
Example #2
0
  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;
  }
Example #3
0
  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
  }