Exemplo n.º 1
0
  private void updatePos(int change) {
    if (change == 0) {
      return;
    }

    boolean isUnderTouch = mPtrIndicator.isUnderTouch();

    // once moved, cancel event will be sent to child
    if (isUnderTouch && !mHasSendCancelEvent && mPtrIndicator.hasMovedAfterPressedDown()) {
      mHasSendCancelEvent = true;
      sendCancelEvent();
    }

    // leave initiated position or just refresh complete
    if ((mPtrIndicator.hasJustLeftStartPosition() && mStatus == PTR_STATUS_INIT)
        || (mPtrIndicator.goDownCrossFinishPosition()
            && mStatus == PTR_STATUS_COMPLETE
            && isEnabledNextPtrAtOnce())) {

      mStatus = PTR_STATUS_PREPARE;
      mPtrUIHandlerHolder.onUIRefreshPrepare(this);
      if (DEBUG) {
        PtrCLog.i(LOG_TAG, "PtrUIHandler: onUIRefreshPrepare, mFlag %s", mFlag);
      }
    }

    // back to initiated position
    if (mPtrIndicator.hasJustBackToStartPosition()) {
      tryToNotifyReset();

      // recover event to children
      if (isUnderTouch) {
        sendDownEvent();
      }
    }

    // Pull to Refresh
    if (mStatus == PTR_STATUS_PREPARE) {
      // reach fresh height while moving from top to bottom
      if (isUnderTouch
          && !isAutoRefresh()
          && mPullToRefresh
          && mPtrIndicator.crossRefreshLineFromTopToBottom()) {
        tryToPerformRefresh();
      }
      // reach header height while auto refresh
      if (performAutoRefreshButLater()
          && mPtrIndicator.hasJustReachedHeaderHeightFromTopToBottom()) {
        tryToPerformRefresh();
      }
    }

    if (DEBUG) {
      PtrCLog.v(
          LOG_TAG,
          "updatePos: change: %s, current: %s last: %s, top: %s, headerHeight: %s",
          change,
          mPtrIndicator.getCurrentPosY(),
          mPtrIndicator.getLastPosY(),
          mContent.getTop(),
          mHeaderHeight);
    }

    mHeaderView.offsetTopAndBottom(change);
    if (!isPinContent()) {
      mContent.offsetTopAndBottom(change);
    }
    invalidate();

    if (mPtrUIHandlerHolder.hasHandler()) {
      mPtrUIHandlerHolder.onUIPositionChange(this, isUnderTouch, mStatus, mPtrIndicator);
    }
    onPositionChange(isUnderTouch, mStatus, mPtrIndicator);
  }
Exemplo n.º 2
0
  @Override
  public boolean dispatchTouchEvent(MotionEvent e) {
    if (!isEnabled() || mContent == null || mHeaderView == null) {
      return dispatchTouchEventSupper(e);
    }
    int action = e.getAction();
    switch (action) {
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        mPtrIndicator.onRelease();
        if (mPtrIndicator.hasLeftStartPosition()) {
          if (DEBUG) {
            PtrCLog.d(LOG_TAG, "call onRelease when user release");
          }
          onRelease(false);
          if (mPtrIndicator.hasMovedAfterPressedDown()) {
            sendCancelEvent();
            return true;
          }
          return dispatchTouchEventSupper(e);
        } else {
          return dispatchTouchEventSupper(e);
        }

      case MotionEvent.ACTION_DOWN:
        mHasSendCancelEvent = false;
        mPtrIndicator.onPressDown(e.getX(), e.getY());

        mScrollChecker.abortIfWorking();

        mPreventForHorizontal = false;
        // The cancel event will be sent once the position is moved.
        // So let the event pass to children.
        // fix #93, #102
        dispatchTouchEventSupper(e);
        return true;

      case MotionEvent.ACTION_MOVE:
        mLastMoveEvent = e;
        mPtrIndicator.onMove(e.getX(), e.getY());
        float offsetX = mPtrIndicator.getOffsetX();
        float offsetY = mPtrIndicator.getOffsetY();

        if (mDisableWhenHorizontalMove
            && !mPreventForHorizontal
            && (Math.abs(offsetX) > mPagingTouchSlop && Math.abs(offsetX) > Math.abs(offsetY))) {
          if (mPtrIndicator.isInStartPosition()) {
            mPreventForHorizontal = true;
          }
        }
        if (mPreventForHorizontal) {
          return dispatchTouchEventSupper(e);
        }

        boolean moveDown = offsetY > 0;
        boolean moveUp = !moveDown;
        boolean canMoveUp = mPtrIndicator.hasLeftStartPosition();

        if (DEBUG) {
          boolean canMoveDown =
              mPtrHandler != null && mPtrHandler.checkCanDoRefresh(this, mContent, mHeaderView);
          PtrCLog.v(
              LOG_TAG,
              "ACTION_MOVE: offsetY:%s, currentPos: %s, moveUp: %s, canMoveUp: %s, moveDown: %s: canMoveDown: %s",
              offsetY,
              mPtrIndicator.getCurrentPosY(),
              moveUp,
              canMoveUp,
              moveDown,
              canMoveDown);
        }

        // disable move when header not reach top
        if (moveDown
            && mPtrHandler != null
            && !mPtrHandler.checkCanDoRefresh(this, mContent, mHeaderView)) {
          return dispatchTouchEventSupper(e);
        }

        if ((moveUp && canMoveUp) || moveDown) {
          movePos(offsetY);
          return true;
        }
    }
    return dispatchTouchEventSupper(e);
  }