@Override
  public final boolean onTouchEvent(MotionEvent event) {

    if (!isPullToRefreshEnabled()) {
      return false;
    }

    // If we're refreshing, and the flag is set. Eat the event
    if (mDisableScrollingWhileRefreshing && isRefreshing()) {
      return true;
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
      return false;
    }

    switch (event.getAction()) {
      case MotionEvent.ACTION_MOVE:
        {
          if (mIsBeingDragged) {
            mLastMotionY = event.getY();
            pullEvent();
            return true;
          }
          break;
        }

      case MotionEvent.ACTION_DOWN:
        {
          if (isReadyForPull()) {
            mLastMotionY = mInitialMotionY = event.getY();
            return true;
          }
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        {
          if (mIsBeingDragged) {
            mIsBeingDragged = false;

            if (mState == RELEASE_TO_REFRESH) {
              if (null != mOnRefreshListener) {
                setRefreshingInternal(true);
                mOnRefreshListener.onRefresh(this);
                return true;

              } else if (null != mOnRefreshListener2) {
                setRefreshingInternal(true);
                if (mCurrentMode == Mode.PULL_DOWN_TO_REFRESH) {
                  mOnRefreshListener2.onPullDownToRefresh(this);
                } else if (mCurrentMode == Mode.PULL_UP_TO_REFRESH) {
                  mOnRefreshListener2.onPullUpToRefresh(this);
                }
                return true;
              } else {
                // If we don't have a listener, just reset
                resetHeader();
                return true;
              }
            }

            smoothScrollTo(0);
            return true;
          }
          break;
        }
    }

    return false;
  }
示例#2
0
  @Override
  public final boolean onTouchEvent(MotionEvent event) {

    if (!isPullToRefreshEnabled()) {
      return false;
    }

    // If we're refreshing, and the flag is set. Eat the event
    if (!mScrollingWhileRefreshingEnabled && isRefreshing()) {
      return true;
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
      return false;
    }

    switch (event.getAction()) {
      case MotionEvent.ACTION_MOVE:
        {
          if (mIsBeingDragged) {
            mLastMotionY = event.getY();
            mLastMotionX = event.getX();
            pullEvent();
            return true;
          }
          break;
        }

      case MotionEvent.ACTION_DOWN:
        {
          if (isReadyForPull()) {
            mLastMotionY = mInitialMotionY = event.getY();
            mLastMotionX = mInitialMotionX = event.getX();
            return true;
          }
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        {
          if (mIsBeingDragged) {
            mIsBeingDragged = false;

            if (mState == State.RELEASE_TO_REFRESH
                && (null != mOnRefreshListener || null != mOnRefreshListener2)) {
              setState(State.REFRESHING, true);
              return true;
            }

            // If we're already refreshing, just scroll back to the top
            if (isRefreshing()) {
              smoothScrollTo(0);
              return true;
            }

            // If we haven't returned by here, then we're not in a state
            // to pull, so just reset
            setState(State.RESET);

            return true;
          }
          break;
        }
    }

    return false;
  }