コード例 #1
0
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    LogUtils.e("ada", "0  onInterceptTouchEvent");

    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
      return true;
    }
    final float x = ev.getX();
    final float y = ev.getY();
    switch (action) {
      case MotionEvent.ACTION_MOVE:
        if (mCurScreen == 1) {
          LinearLayout view = (LinearLayout) getChildAt(1);
          if (y < view.getMeasuredHeight()) {
            ScrollLayout1 l = (ScrollLayout1) view.getChildAt(0);
            int curscreen = l.getCurScreen();
            curscreen++;
            int deltaX = (int) (mLastMotionX - x);
            if (curscreen == 1) {
              if (deltaX < 0) {
                return true;
              } else {
                return false;
              }
            } else {
              if (curscreen == l.getChildCount()) {
                if (deltaX > 0) {
                  return true;
                } else {
                  return false;
                }
              } else {
                return false;
              }
            }
          }
        }
        LogUtils.e(TAG, "0  onInterceptTouchEvent-ACTION_MOVE");
        final int xDiff = (int) Math.abs(mLastMotionX - x);
        if (xDiff > mTouchSlop) {
          mTouchState = TOUCH_STATE_SCROLLING;
        }
        break;
      case MotionEvent.ACTION_DOWN:
        mLastMotionX = x;
        mLastMotionY = y;
        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        break;
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        mTouchState = TOUCH_STATE_REST;
        break;
    }
    return mTouchState != TOUCH_STATE_REST;
  }
コード例 #2
0
 public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   mScroller = new Scroller(context);
   mCurScreen = mDefaultScreen;
   mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
   LogUtils.setLoggerLevel(LogUtils.ERROR);
 }
コード例 #3
0
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    LogUtils.e("ada", "0  onTouchEvent");

    // 是否可滑动
    if (!isScroll) {
      return false;
    }
    Launcher.view.onTouchEvent(event);

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    final int action = event.getAction();
    final float x = event.getX();
    final float y = event.getY();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        // LogUtils.e(TAG, "event down!");
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }
        mLastMotionX = x;

        // ---------------New Code----------------------
        mLastMotionY = y;
        // ---------------------------------------------
        return true;
      case MotionEvent.ACTION_MOVE:
        LogUtils.e(TAG, "0  onTouchEvent-ACTION_MOVE");
        int deltaX = (int) (mLastMotionX - x);

        // ---------------New Code----------------------
        int deltaY = (int) (mLastMotionY - y);
        if (Math.abs(deltaX) < 200 && Math.abs(deltaY) > 10) break;
        mLastMotionY = y;
        // -------------------------------------

        mLastMotionX = x;
        scrollBy(deltaX, 0);
        break;
      case MotionEvent.ACTION_UP:
        // LogUtils.e(TAG, "event : up");
        // if (mTouchState == TOUCH_STATE_SCROLLING) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000);
        int velocityX = (int) velocityTracker.getXVelocity();
        // LogUtils.e(TAG, "velocityX:" + velocityX);
        if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
          // Fling enough to move left
          // LogUtils.e(TAG, "snap left");
          snapToScreen(mCurScreen - 1);
        } else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) {
          // Fling enough to move right
          // LogUtils.e(TAG, "snap right");
          snapToScreen(mCurScreen + 1);
        } else {
          snapToDestination();
        }
        if (mVelocityTracker != null) {
          mVelocityTracker.recycle();
          mVelocityTracker = null;
        }
        // }
        mTouchState = TOUCH_STATE_REST;
        break;
      case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
        break;
    }

    return true;
  }