Example #1
0
  @Override
  protected void layoutChildren() {
    super.layoutChildren();
    if (mRequestedScrollPosition == -1) {
      return;
    }

    final int position = mRequestedScrollPosition;
    mRequestedScrollPosition = -1;

    int firstPosition = getFirstVisiblePosition() + 1;
    int lastPosition = getLastVisiblePosition();
    if (position >= firstPosition && position <= lastPosition) {
      return; // Already on screen
    }

    final int offset = (int) (getHeight() * PREFERRED_SELECTION_OFFSET_FROM_TOP);
    if (!mSmoothScrollRequested) {
      setSelectionFromTop(position, offset);

      // Since we have changed the scrolling position, we need to redo child layout
      // Calling "requestLayout" in the middle of a layout pass has no effect,
      // so we call layoutChildren explicitly
      super.layoutChildren();

    } else {
      // We will first position the list a couple of screens before or after
      // the new selection and then scroll smoothly to it.
      int twoScreens = (lastPosition - firstPosition) * 2;
      int preliminaryPosition;
      if (position < firstPosition) {
        preliminaryPosition = position + twoScreens;
        if (preliminaryPosition >= getCount()) {
          preliminaryPosition = getCount() - 1;
        }
        if (preliminaryPosition < firstPosition) {
          setSelection(preliminaryPosition);
          super.layoutChildren();
        }
      } else {
        preliminaryPosition = position - twoScreens;
        if (preliminaryPosition < 0) {
          preliminaryPosition = 0;
        }
        if (preliminaryPosition > lastPosition) {
          setSelection(preliminaryPosition);
          super.layoutChildren();
        }
      }

      smoothScrollToPositionFromTop(position, offset);
    }
  }
  @Override
  protected void layoutChildren() {

    // we need to control calls to layoutChildren while
    // dragging to prevent things happening out of order
    if (mFloatView == null) {
      super.layoutChildren();
    }
  }
 @Override
 protected void layoutChildren() {
   if (!mBlockLayoutChildren && !mBlockLayoutAndMeasure) {
     super.layoutChildren();
   }
 }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (mGestureDetector != null) {
      mGestureDetector.onTouchEvent(ev);
    }
    if ((mDragListener != null || mDropListener != null) && mFloatView != null) {
      int action = ev.getAction();

      final int x = (int) ev.getX();
      final int y = (int) ev.getY();

      switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
          Rect r = mTempRect;
          mFloatView.getDrawingRect(r);
          // mDragScroller.stopScrolling(true);

          if (mRemoveMode == SLIDE && ev.getX() > r.right * 3 / 4) {
            dropFloatView(true);
          } else {
            dropFloatView(false);
          }

          break;

        case MotionEvent.ACTION_DOWN:
          // doExpansion();
          break;
        case MotionEvent.ACTION_MOVE:

          // make src item invisible on first move away from pickup
          // point. Reduces flicker.
          if (mLastY == mDownY) {
            // should we be this careful?
            final View item = getChildAt(mSrcDragPos - getFirstVisiblePosition());
            if (item != null) {
              item.setVisibility(INVISIBLE);
            }
          }

          dragView(x, y);

          if (!mDragScroller.isScrolling()) {
            final int first = getFirstVisiblePosition();
            final View startView = getChildAt(mExpDragPos - first);
            int startPos;
            int startTop;
            if (startView == null) {
              startPos = first + getChildCount() / 2;
              startTop = getChildAt(startPos - first).getTop();
              Log.d("mobeta", "startView was null");
            } else {
              startPos = mExpDragPos;
              startTop = startView.getTop();
            }

            // Log.d("mobeta", "move shuffle");
            boolean shuffled = shuffleItems(getFloatPosition(y, startPos, startTop));

            if (shuffled) {
              super.layoutChildren();
            }
          }

          // get the current scroll direction
          int currentScrollDir = mDragScroller.getScrollDir();

          if (y > mLastY && y > mDownScrollStartY && currentScrollDir != DragScroller.DOWN) {
            // dragged down, it is below the down scroll start and
            // it is not scrolling up

            if (currentScrollDir != DragScroller.STOP) {
              // moved directly from up scroll to down scroll
              mDragScroller.stopScrolling(true);
            }

            // start scrolling down
            mDragScroller.startScrolling(DragScroller.DOWN);
          } else if (y < mLastY && y < mUpScrollStartY && currentScrollDir != DragScroller.UP) {
            // dragged up, it is above the up scroll start and it is
            // not scrolling up

            if (currentScrollDir != DragScroller.STOP) {
              // moved directly from down scroll to up scroll
              mDragScroller.stopScrolling(true);
            }

            // start scrolling up
            mDragScroller.startScrolling(DragScroller.UP);
          } else if (y >= mUpScrollStartY
              && y <= mDownScrollStartY
              && mDragScroller.isScrolling()) {
            // not in the upper nor in the lower drag-scroll regions
            // but it is still scrolling

            mDragScroller.stopScrolling(true);
          }
          break;
      }

      mLastX = x;
      mLastY = y;

      return true;
    }
    return super.onTouchEvent(ev);
  }