Example #1
0
  /**
   * Actions a Pull Event
   *
   * @return true if the Event has been handled, false if there has been no change
   */
  private void pullEvent() {
    final int newScrollValue;
    final int itemDimension;
    final float initialMotionValue, lastMotionValue;

    switch (getPullToRefreshScrollDirection()) {
      case HORIZONTAL:
        initialMotionValue = mInitialMotionX;
        lastMotionValue = mLastMotionX;
        break;
      case VERTICAL:
      default:
        initialMotionValue = mInitialMotionY;
        lastMotionValue = mLastMotionY;
        break;
    }

    switch (mCurrentMode) {
      case PULL_FROM_END:
        newScrollValue = Math.round(Math.max(initialMotionValue - lastMotionValue, 0) / FRICTION);
        itemDimension = getFooterSize();
        break;
      case PULL_FROM_START:
      default:
        newScrollValue = Math.round(Math.min(initialMotionValue - lastMotionValue, 0) / FRICTION);
        itemDimension = getHeaderSize();
        break;
    }

    setHeaderScroll(newScrollValue);

    if (newScrollValue != 0 && !isRefreshing()) {
      float scale = Math.abs(newScrollValue) / (float) itemDimension;
      switch (mCurrentMode) {
        case PULL_FROM_END:
          mFooterLayout.onPull(scale);
          break;
        case PULL_FROM_START:
        default:
          mHeaderLayout.onPull(scale);
          break;
      }

      if (mState != State.PULL_TO_REFRESH && itemDimension >= Math.abs(newScrollValue)) {
        setState(State.PULL_TO_REFRESH);
      } else if (mState == State.PULL_TO_REFRESH && itemDimension < Math.abs(newScrollValue)) {
        setState(State.RELEASE_TO_REFRESH);
      }
    }
  }
  /**
   * Actions a Pull Event
   *
   * @return true if the Event has been handled, false if there has been no change
   */
  private boolean pullEvent() {

    final int newHeight;
    final int oldHeight = getScrollY();

    switch (mCurrentMode) {
      case PULL_UP_TO_REFRESH:
        newHeight = Math.round(Math.max(mInitialMotionY - mLastMotionY, 0) / FRICTION);
        break;
      case PULL_DOWN_TO_REFRESH:
      default:
        newHeight = Math.round(Math.min(mInitialMotionY - mLastMotionY, 0) / FRICTION);
        break;
    }

    setHeaderScroll(newHeight);

    if (newHeight != 0) {

      float scale = Math.abs(newHeight) / (float) mHeaderHeight;
      switch (mCurrentMode) {
        case PULL_UP_TO_REFRESH:
          mFooterLayout.onPullY(scale);
          break;
        case PULL_DOWN_TO_REFRESH:
          mHeaderLayout.onPullY(scale);
          break;
      }

      if (mState == PULL_TO_REFRESH && mHeaderHeight < Math.abs(newHeight)) {
        mState = RELEASE_TO_REFRESH;
        onReleaseToRefresh();
        return true;

      } else if (mState == RELEASE_TO_REFRESH && mHeaderHeight >= Math.abs(newHeight)) {
        mState = PULL_TO_REFRESH;
        onPullToRefresh();
        return true;
      }
    }

    return oldHeight != newHeight;
  }
  /**
   * Helper method for Overscrolling that encapsulates all of the necessary function. This is the
   * advanced version of the call.
   *
   * @param view - PullToRefreshView that is calling this.
   * @param deltaX - Change in X in pixels, passed through from from overScrollBy call
   * @param scrollX - Current X scroll value in pixels before applying deltaY, passed through from
   *     from overScrollBy call
   * @param deltaY - Change in Y in pixels, passed through from from overScrollBy call
   * @param scrollY - Current Y scroll value in pixels before applying deltaY, passed through from
   *     from overScrollBy call
   * @param scrollRange - Scroll Range of the View, specifically needed for ScrollView
   * @param fuzzyThreshold - Threshold for which the values how fuzzy we should treat the other
   *     values. Needed for WebView as it doesn't always scroll back to it's edge. 0 = no fuzziness.
   * @param scaleFactor - Scale Factor for overscroll amount
   * @param isTouchEvent - true if this scroll operation is the result of a touch event, passed
   *     through from from overScrollBy call
   */
  public static void overScrollBy(
      final PullToRefreshBase<?> view,
      final int deltaX,
      final int scrollX,
      final int deltaY,
      final int scrollY,
      final int scrollRange,
      final int fuzzyThreshold,
      final float scaleFactor,
      final boolean isTouchEvent) {

    final int deltaValue, currentScrollValue, scrollValue;
    switch (view.getPullToRefreshScrollDirection()) {
      case PullToRefreshBase.HORIZONTAL_SCROLL:
        deltaValue = deltaX;
        scrollValue = scrollX;
        currentScrollValue = view.getScrollX();
        break;
      case PullToRefreshBase.VERTICAL_SCROLL:
      default:
        deltaValue = deltaY;
        scrollValue = scrollY;
        currentScrollValue = view.getScrollY();
        break;
    }

    // Check that OverScroll is enabled
    if (view.isPullToRefreshOverScrollEnabled()) {
      final Mode mode = view.getMode();

      // Check that Pull-to-Refresh is enabled, and the event isn't from
      // touch
      if (mode.permitsPullToRefresh() && !isTouchEvent && deltaValue != 0) {
        final int newScrollValue = (deltaValue + scrollValue);

        if (PullToRefreshBase.DEBUG) {
          Log.d(
              LOG_TAG,
              "OverScroll. DeltaX: "
                  + deltaX
                  + ", ScrollX: "
                  + scrollX
                  + ", DeltaY: "
                  + deltaY
                  + ", ScrollY: "
                  + scrollY
                  + ", NewY: "
                  + newScrollValue
                  + ", ScrollRange: "
                  + scrollRange
                  + ", CurrentScroll: "
                  + currentScrollValue);
        }

        if (newScrollValue < (0 - fuzzyThreshold)) {
          // Check the mode supports the overscroll direction, and
          // then move scroll
          if (mode.showHeaderLoadingLayout()) {
            view.setHeaderScroll((int) (scaleFactor * (currentScrollValue + newScrollValue)));
          }
        } else if (newScrollValue > (scrollRange + fuzzyThreshold)) {
          // Check the mode supports the overscroll direction, and
          // then move scroll
          if (mode.showFooterLoadingLayout()) {
            view.setHeaderScroll(
                (int) (scaleFactor * (currentScrollValue + newScrollValue - scrollRange)));
          }
        } else if (Math.abs(newScrollValue) <= fuzzyThreshold
            || Math.abs(newScrollValue - scrollRange) <= fuzzyThreshold) {
          // Means we've stopped overscrolling, so scroll back to 0
          view.smoothScrollToLonger(0);
        }
      }
    }
  }