Beispiel #1
0
  public void onRequireTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    int scrollX = getScrollX();
    Log.d(TAG, "x=" + x + "  y=" + y);

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        {
          if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
          }
          if (mOnSlideListener != null) {
            mOnSlideListener.onSlide(this, OnSlideListener.SLIDE_STATUS_START_SCROLL);
          }
          break;
        }
      case MotionEvent.ACTION_MOVE:
        {
          int deltaX = x - mLastX;
          int deltaY = y - mLastY;
          if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) {
            break;
          }

          int newScrollX = scrollX - deltaX;
          if (deltaX != 0) {
            if (newScrollX < 0) {
              newScrollX = 0;
            } else if (newScrollX > mHolderWidth) {
              newScrollX = mHolderWidth;
            }
            this.scrollTo(newScrollX, 0);
          }
          break;
        }
      case MotionEvent.ACTION_UP:
        {
          int newScrollX = 0;
          if (scrollX - mHolderWidth * 0.75 > 0) {
            newScrollX = mHolderWidth;
          }
          this.smoothScrollTo(newScrollX, 0);
          if (mOnSlideListener != null) {
            mOnSlideListener.onSlide(
                this,
                newScrollX == 0
                    ? OnSlideListener.SLIDE_STATUS_OFF
                    : OnSlideListener.SLIDE_STATUS_ON);
          }
          break;
        }
      default:
        break;
    }

    mLastX = x;
    mLastY = y;
  }
  public boolean closeImmediately() {
    if (!isOpened() || mAlwaysOpened || mMode == MODE_SLIDE) {
      return false;
    }

    if (!isReadyForSlide()) {
      mWhenReady.add(
          new Runnable() {

            @Override
            public void run() {
              closeImmediately();
            }
          });

      return true;
    }

    mMenuView.setVisibility(View.GONE);
    mMode = MODE_READY;
    requestLayout();

    if (mListener != null) {
      mListener.onSlideCompleted(false);
    }

    return true;
  }
  private void completeClosing() {
    mOffset = 0;
    requestLayout();

    post(
        new Runnable() {

          @Override
          public void run() {
            mMode = MODE_READY;
            mMenuView.setVisibility(View.GONE);
          }
        });

    if (mListener != null) {
      mListener.onSlideCompleted(false);
    }
  }
  private void completeOpening() {
    mOffset = mDirection * mMenuView.getWidth();
    requestLayout();

    post(
        new Runnable() {

          @Override
          public void run() {
            mMode = MODE_FINISHED;
            mMenuView.setVisibility(View.VISIBLE);
          }
        });

    if (mListener != null) {
      mListener.onSlideCompleted(true);
    }
  }