Exemple #1
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_velocityTracker != null)
         {
             _velocityTracker.Recycle();
             _velocityTracker = null;
         }
     }
     base.Dispose(disposing);
 }
 private void Cancel()
 {
     _view.Animate()
     .TranslationX(0)
     .Alpha(1)
     .SetDuration(_animationTime)
     .SetListener(null);
     _velocityTracker.Recycle();
     _velocityTracker = null;
     _translationX    = 0;
     _downX           = 0;
     _downY           = 0;
     _swiping         = false;
 }
        public bool onTouch(MotionEvent ev)
        {
            if (velocityTracker == null)
            {
                velocityTracker = VelocityTracker.Obtain();
            }

            velocityTracker.AddMovement(ev);

            if (ev.Action == MotionEventActions.Down)
            {
                if (!scroller.IsFinished)
                {
                    scroller.AbortAnimation();
                }

                isSmoothScrolling = false;
            }
            else if (ev.Action == MotionEventActions.Move)
            {
                velocityTracker.AddMovement(ev);
                velocityTracker.ComputeCurrentVelocity(500);
            }
            else if (ev.Action == MotionEventActions.Up)
            {
                handleHorizontalScrolling();
                velocityTracker.Recycle();
                velocityTracker.Clear();
                velocityTracker = null;
                isScrolling     = false;
            }

            return(false);
        }
            public override bool OnTouchEvent(MotionEvent ev)
            {
                switch (ev.Action)
                {
                case MotionEventActions.Cancel:
                    #region Cancel
                    if (null != mVelocityTracker)
                    {
                        mVelocityTracker.Recycle();
                        mVelocityTracker = null;
                    }
                    #endregion
                    break;

                case MotionEventActions.Down:
                    #region Down
                    mVelocityTracker = VelocityTracker.Obtain();
                    mVelocityTracker.AddMovement(ev);

                    mLastTouchX = GetActiveX(ev);
                    mLastTouchY = GetActiveY(ev);
                    mIsDragging = false;
                    #endregion
                    break;

                case MotionEventActions.Move:
                    #region Move
                    float x = GetActiveX(ev);
                    float y = GetActiveY(ev);
                    float dx = x - mLastTouchX, dy = y - mLastTouchY;

                    if (!mIsDragging)
                    {
                        // Use Pythagoras to see if drag length is larger than
                        // touch slop
                        mIsDragging = Math.Sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
                    }

                    if (mIsDragging)
                    {
                        mListener.OnDrag(dx, dy);
                        mLastTouchX = x;
                        mLastTouchY = y;

                        if (null != mVelocityTracker)
                        {
                            mVelocityTracker.AddMovement(ev);
                        }
                    }
                    #endregion
                    break;
                }

                return(true);
            }
        private void EndDrag()
        {
            _quickReturn = false;
            _isBeingDragged = false;
            _isUnableToDrag = false;
            ActivePointerId = InvalidPointer;

            if (VelocityTracker == null) return;
            VelocityTracker.Recycle();
            VelocityTracker = null;
        }
        private void EndDrag()
        {
            mQuickReturn     = false;
            mIsBeingDragged  = false;
            mIsUnableToDrag  = false;
            mActivePointerId = INVALID_POINTER;

            if (mVelocityTracker != null)
            {
                mVelocityTracker.Recycle();
                mVelocityTracker = null;
            }
        }
Exemple #7
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            if (mVelocityTracker == null)
            {
                mVelocityTracker = VelocityTracker.Obtain();
            }
            mVelocityTracker.AddMovement(e);

            MotionEventActions action = e.Action;
            float x = e.GetX();

            switch (action)
            {
            case MotionEventActions.Down:
                if (!mScroller.IsFinished)
                {
                    mScroller.AbortAnimation();
                }

                mLastMotionX = x;

                if (mScroller.IsFinished)
                {
                    mTouchState = TOUCH_STATE_REST;
                }
                else
                {
                    mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING;
                }
                break;

            case MotionEventActions.Move:
                int  xDiff  = (int)Math.Abs(x - mLastMotionX);
                bool xMoved = xDiff > mTouchSlop;

                if (xMoved)
                {
                    mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING;
                }

                if (mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING)
                {
                    int deltaX = (int)(mLastMotionX - x);
                    mLastMotionX = x;
                    int scrollX = this.ScrollX;

                    if (deltaX < 0)
                    {
                        if (scrollX > 0)
                        {
                            ScrollBy(Math.Max(-scrollX, deltaX), 0);
                        }
                    }
                    else if (deltaX > 0)
                    {
                        if (this.ChildCount >= 1)
                        {
                            int avalableToScroll = this.GetChildAt(this.ChildCount - 1).Right - scrollX - Width;

                            if (avalableToScroll > 0)
                            {
                                ScrollBy(Math.Min(avalableToScroll, deltaX), 0);
                            }
                        }
                    }
                }
                break;

            case MotionEventActions.Up:
                if (mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING)
                {
                    VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.ComputeCurrentVelocity(VELOCITY_UNIT_PIXELS_PER_SECOND, mMaximumVelocity);
                    int velocityX = (int)velocityTracker.XVelocity;

                    if (velocityX > mDensityAdjustedSnapVelocity && mCurrentScreen > 0)
                    {
                        SnapToScreen(mCurrentScreen - 1);
                    }
                    else if (velocityX < -mDensityAdjustedSnapVelocity && mCurrentScreen < this.ChildCount - 1)
                    {
                        SnapToScreen(mCurrentScreen + 1);
                    }
                    else
                    {
                        SnapToDestination();
                    }

                    if (mVelocityTracker != null)
                    {
                        mVelocityTracker.Recycle();
                        mVelocityTracker = null;
                    }
                    mTouchState = TOUCH_STATE_REST;
                }
                break;

            case MotionEventActions.Cancel:
                mTouchState = TOUCH_STATE_REST;
                break;

            default:
                break;
            }
            return(true);
        }
Exemple #8
0
        public bool OnTouch(View view, MotionEvent motionEvent)
        {
            if (mViewWidth < 2)
            {
                mViewWidth = mListView.Width;
            }

            switch (motionEvent.ActionMasked)
            {
            case MotionEventActions.Down:
                if (mPaused)
                {
                    return(false);
                }
                // TODO: ensure this is a finger, and set a flag
                // Find the child view that was touched (perform a hit test)
                Rect  rect           = new Rect();
                int   childCount     = mListView.ChildCount;
                int[] listViewCoords = new int[2];
                mListView.GetLocationOnScreen(listViewCoords);
                int  x = (int)motionEvent.RawX - listViewCoords [0];
                int  y = (int)motionEvent.RawY - listViewCoords [1];
                View child;
                for (int i = 0; i < childCount; i++)
                {
                    child = mListView.GetChildAt(i);
                    child.GetHitRect(rect);
                    if (rect.Contains(x, y))
                    {
                        mDownView = child;
                        break;
                    }
                }

                if (mDownView != null)
                {
                    mDownX        = motionEvent.RawX;
                    mDownY        = motionEvent.RawY;
                    mDownPosition = mListView.GetPositionForView(mDownView);
                    if (mCallbacks.canDismiss(mDownPosition))
                    {
                        mVelocityTracker = VelocityTracker.Obtain();
                        mVelocityTracker.AddMovement(motionEvent);
                    }
                    else
                    {
                        mDownView = null;
                    }
                }
                return(false);

            case MotionEventActions.Cancel: {
                if (mVelocityTracker == null)
                {
                    break;
                }

                if (mDownView != null && mSwiping)
                {
                    // cancel
                    mDownView.Animate()
                    .TranslationX(0)
                    .Alpha(1)
                    .SetDuration(mAnimationTime)
                    .SetListener(null);
                }
                mVelocityTracker.Recycle();
                mVelocityTracker = null;
                mDownX           = 0;
                mDownY           = 0;
                mDownView        = null;
                mDownPosition    = ListView.InvalidPosition;
                mSwiping         = false;
                break;
            }

            case MotionEventActions.Up: {
                if (mVelocityTracker == null)
                {
                    break;
                }

                float deltaX = motionEvent.RawX - mDownX;
                mVelocityTracker.AddMovement(motionEvent);
                mVelocityTracker.ComputeCurrentVelocity(1000);
                float velocityX    = mVelocityTracker.XVelocity;
                float absVelocityX = Math.Abs(velocityX);
                float absVelocityY = Math.Abs(mVelocityTracker.YVelocity);
                bool  dismiss      = false;
                bool  dismissRight = false;
                if (Math.Abs(deltaX) > mViewWidth / 2)
                {
                    dismiss      = true;
                    dismissRight = deltaX > 0;
                }
                else
                if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping)
                {
                    // dismiss only if flinging in the same direction as dragging
                    dismiss      = (velocityX < 0) == (deltaX < 0);
                    dismissRight = mVelocityTracker.XVelocity > 0;
                }
                if (dismiss && mDownPosition != ListView.InvalidPosition)
                {
                    // dismiss
                    View downView     = mDownView;                         // mDownView gets null'd before animation ends
                    int  downPosition = mDownPosition;
                    ++mDismissAnimationRefCount;
                    var anim = mDownView.Animate()
                               .TranslationX(dismissRight ? mViewWidth : -mViewWidth)
                               .Alpha(0)
                               .SetDuration(mAnimationTime)
                               .SetListener(new DownAnimatorListenerAdapter(this, downView, downPosition));
                }

                else
                {
                    // cancel
                    mDownView.Animate()
                    .TranslationX(0)
                    .Alpha(1)
                    .SetDuration(mAnimationTime)
                    .SetListener(null);
                }
                mVelocityTracker.Recycle();
                mVelocityTracker = null;
                mDownX           = 0;
                mDownY           = 0;
                mDownView        = null;
                mDownPosition    = ListView.InvalidPosition;
                mSwiping         = false;
                break;
            }

            case MotionEventActions.Move: {
                if (mVelocityTracker == null || mPaused)
                {
                    break;
                }

                mVelocityTracker.AddMovement(motionEvent);
                float deltaX = motionEvent.RawX - mDownX;
                float deltaY = motionEvent.RawY - mDownY;
                if (Math.Abs(deltaX) > mSlop && Math.Abs(deltaY) < Math.Abs(deltaX) / 2)
                {
                    mSwiping     = true;
                    mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
                    mListView.RequestDisallowInterceptTouchEvent(true);

                    // Cancel ListView's touch (un-highlighting the item)
                    MotionEvent cancelEvent = MotionEvent.Obtain(motionEvent);
                    cancelEvent.Action = (Android.Views.MotionEventActions)((int)MotionEventActions.Cancel |
                                                                            ((int)motionEvent.ActionIndex << (int)MotionEventActions.PointerIndexShift));
                    mListView.OnTouchEvent(cancelEvent);
                    cancelEvent.Recycle();
                }

                if (mSwiping)
                {
                    mDownView.TranslationX = deltaX - mSwipingSlop;
                    mDownView.Alpha        = Math.Max(0f, Math.Min(1f, 1f - 2f * Math.Abs(deltaX) / mViewWidth));
                    return(true);
                }
                break;
            }
            }
            return(false);
        }
        public virtual bool OnTouchEvent(Android.Views.MotionEvent ev)
        {
            switch (ev.Action)
            {
            case MotionEventActions.Down:
            {
                mVelocityTracker = VelocityTracker.Obtain();
                if (null != mVelocityTracker)
                {
                    mVelocityTracker.AddMovement(ev);
                }
                else
                {
                    Log.Info(LOG_TAG, "Velocity tracker is null");
                }

                mLastTouchX = GetActiveX(ev);
                mLastTouchY = GetActiveY(ev);
                mIsDragging = false;
                break;
            }

            case MotionEventActions.Move: {
                float x = GetActiveX(ev);
                float y = GetActiveY(ev);
                float dx = x - mLastTouchX, dy = y - mLastTouchY;

                if (!mIsDragging)
                {
                    // Use Pythagoras to see if drag length is larger than
                    // touch slop
                    mIsDragging = FloatMath.Sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
                }

                if (mIsDragging)
                {
                    mListener.OnDrag(dx, dy);
                    mLastTouchX = x;
                    mLastTouchY = y;

                    if (null != mVelocityTracker)
                    {
                        mVelocityTracker.AddMovement(ev);
                    }
                }
                break;
            }

            case MotionEventActions.Cancel: {
                // Recycle Velocity Tracker
                if (null != mVelocityTracker)
                {
                    mVelocityTracker.Recycle();
                    mVelocityTracker = null;
                }
                break;
            }

            case MotionEventActions.Up: {
                if (mIsDragging)
                {
                    if (null != mVelocityTracker)
                    {
                        mLastTouchX = GetActiveX(ev);
                        mLastTouchY = GetActiveY(ev);

                        // Compute velocity within the last 1000ms
                        mVelocityTracker.AddMovement(ev);
                        mVelocityTracker.ComputeCurrentVelocity(1000);

                        float vX = mVelocityTracker.GetXVelocity(0), vY = mVelocityTracker
                                                                          .GetYVelocity(0);

                        // If the velocity is greater than minVelocity, call
                        // listener
                        if (Math.Max(Math.Abs(vX), Math.Abs(vY)) >= mMinimumVelocity)
                        {
                            mListener.OnFling(mLastTouchX, mLastTouchY, -vX,
                                              -vY);
                        }
                    }
                }

                // Recycle Velocity Tracker
                if (null != mVelocityTracker)
                {
                    mVelocityTracker.Recycle();
                    mVelocityTracker = null;
                }
                break;
            }
            }

            return(true);
        }
        public override bool OnInterceptTouchEvent(MotionEvent ev)
        {
            /*
             * This method JUST determines whether we want to intercept the motion.
             * If we return true, onTouchEvent will be called and we do the actual
             * scrolling there.
             */

            // Begin tracking velocity even before we have intercepted touch events.
            if (mVelocityTracker == null)
            {
                mVelocityTracker = VelocityTracker.Obtain();
            }
            mVelocityTracker.AddMovement(ev);

            /*
             * Shortcut the most recurring case: the user is in the dragging
             * state and he is moving his finger.  We want to intercept this
             * motion.
             */
            var action = ev.Action;

            if (mIsVerbose)
            {
                Log.Verbose(TAG, "onInterceptTouchEvent: " + (ev.Action & MotionEventActions.Mask));
            }
            if (((action & MotionEventActions.Mask) == MotionEventActions.Move) &&
                (mTouchState == TOUCH_STATE_SCROLLING))
            {
                if (mIsVerbose)
                {
                    Log.Verbose(TAG, "Intercepting touch events");
                }
                return(true);
            }

            switch (action & MotionEventActions.Mask)
            {
            case MotionEventActions.Move: {
                if (mLocked)
                {
                    // we're locked on the current screen, don't allow moving
                    break;
                }

                /*
                 * Locally do absolute value. mDownMotionX is set to the y value
                 * of the down event.
                 */
                int   pointerIndex = MotionEventCompat.FindPointerIndex(ev, mActivePointerId);
                float x            = MotionEventCompat.GetX(ev, pointerIndex);
                float y            = MotionEventCompat.GetY(ev, pointerIndex);
                int   xDiff        = (int)Java.Lang.Math.Abs(x - mDownMotionX);
                int   yDiff        = (int)Java.Lang.Math.Abs(y - mDownMotionY);

                bool xPaged = xDiff > mPagingTouchSlop;
                bool xMoved = xDiff > mTouchSlop;
                bool yMoved = yDiff > mTouchSlop;

                if (xMoved || yMoved)
                {
                    if (xPaged)
                    {
                        // Scroll if the user moved far enough along the X axis
                        mTouchState = TOUCH_STATE_SCROLLING;
                    }
                    // Either way, cancel any pending longpress
                    if (mAllowLongPress)
                    {
                        mAllowLongPress = false;
                        // Try canceling the long press. It could also have been scheduled
                        // by a distant descendant, so use the mAllowLongPress flag to block
                        // everything
                        View currentScreen = GetScreenAt(mCurrentScreen);
                        if (currentScreen != null)
                        {
                            currentScreen.CancelLongPress();
                        }
                    }
                }
                break;
            }

            case MotionEventActions.Down: {
                float x = ev.GetX();
                float y = ev.GetY();
                // Remember location of down touch
                mDownMotionX     = x;
                mDownMotionY     = y;
                mDownScrollX     = ScrollX;
                mActivePointerId = MotionEventCompat.GetPointerId(ev, 0);
                mAllowLongPress  = true;

                /*
                 * If being flinged and user touches the screen, initiate drag;
                 * otherwise don't.  mScroller.isFinished should be false when
                 * being flinged.
                 */
                mTouchState = mScroller.IsFinished ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
                break;
            }

            case MotionEventActions.Cancel:
            case MotionEventActions.Up:
                // Release the drag
                mTouchState      = TOUCH_STATE_REST;
                mAllowLongPress  = false;
                mActivePointerId = INVALID_POINTER;
                if (mVelocityTracker == null)
                {
                    mVelocityTracker.Recycle();
                    mVelocityTracker = null;
                }
                break;



            case MotionEventActions.PointerUp:
                OnSecondaryPointerUp(ev);
                break;
            }

            /*
             * The only time we want to intercept motion events is if we are in the
             * drag mode.
             */
            bool intercept = mTouchState != TOUCH_STATE_REST;

            if (mIsVerbose)
            {
                Log.Verbose(TAG, "Intercepting touch events: " + intercept);
            }
            return(intercept);
        }
        public override bool OnTouchEvent(MotionEvent ev)
        {
            if (_velocityTracker == null)
            {
                _velocityTracker = VelocityTracker.Obtain();
            }
            _velocityTracker.AddMovement(ev);

            var action = ev.Action;
            var x      = ev.GetX();

            switch (action)
            {
            case MotionEventActions.Down:
                /*
                 * If being flinged and user touches, stop the fling. isFinished will be false if being flinged.
                 */
                if (!_scroller.IsFinished)
                {
                    _scroller.AbortAnimation();
                }

                // Remember where the motion event started
                _lastMotionX = x;

                _touchState = _scroller.IsFinished ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;

                break;

            case MotionEventActions.Move:
                var xDiff = (int)Math.Abs(x - _lastMotionX);

                var xMoved = xDiff > _touchSlop;

                if (xMoved)
                {
                    // Scroll if the user moved far enough along the X axis
                    _touchState = TOUCH_STATE_SCROLLING;
                }

                if (_touchState == TOUCH_STATE_SCROLLING)
                {
                    // Scroll to follow the motion event
                    var deltaX = (int)(_lastMotionX - x);
                    _lastMotionX = x;

                    var scrollX = ScrollX;
                    if (deltaX < 0)
                    {
                        if (scrollX > 0)
                        {
                            ScrollBy(Math.Max(-scrollX, deltaX), 0);
                        }
                    }
                    else if (deltaX > 0)
                    {
                        var availableToScroll = GetChildAt(ChildCount - 1).Right - scrollX - Width;
                        if (availableToScroll > 0)
                        {
                            ScrollBy(Math.Min(availableToScroll, deltaX), 0);
                        }
                    }
                }
                break;

            case MotionEventActions.Up:
                if (_touchState == TOUCH_STATE_SCROLLING)
                {
                    var velocityTracker = _velocityTracker;
                    velocityTracker.ComputeCurrentVelocity(1000, _maximumVelocity);
                    var velocityX = (int)velocityTracker.XVelocity;

                    if (velocityX > SNAP_VELOCITY && _currentScreen > 0)
                    {
                        // Fling hard enough to move left
                        SnapToScreen(_currentScreen - 1);
                    }
                    else if (velocityX < -SNAP_VELOCITY && _currentScreen < ChildCount - 1)
                    {
                        // Fling hard enough to move right
                        SnapToScreen(_currentScreen + 1);
                    }
                    else
                    {
                        SnapToDestination();
                    }

                    if (_velocityTracker != null)
                    {
                        _velocityTracker.Recycle();
                        _velocityTracker = null;
                    }
                }

                _touchState = TOUCH_STATE_REST;

                break;

            case MotionEventActions.Cancel:
                _touchState = TOUCH_STATE_REST;
                break;
            }

            return(true);
        }
Exemple #12
0
        public override bool OnTouchEvent(MotionEvent ev)
        {
            if (_mVelocityTracker == null)
            {
                _mVelocityTracker = VelocityTracker.Obtain();
            }
            _mVelocityTracker.AddMovement(ev);


            MotionEventActions action = ev.Action;
            float x = ev.GetX();


            switch (action)
            {
            case MotionEventActions.Down:
                /*
                 * If being flinged and user touches, stop the fling. isFinished will be false if
                 * being flinged.
                 */
                if (!_mScroller.IsFinished)
                {
                    _mScroller.AbortAnimation();
                }


                // Remember where the motion event started
                _mLastMotionX = x;


                _mTouchState = _mScroller.IsFinished ? TOUCH_STATE_REST : TOUCH_STATE_HORIZONTAL_SCROLLING;


                break;

            case MotionEventActions.Move:
                var  xDiff  = (int)Math.Abs(x - _mLastMotionX);
                bool xMoved = xDiff > _mTouchSlop;


                if (xMoved)
                {
                    // Scroll if the user moved far enough along the X axis
                    _mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING;
                }


                if (_mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING)
                {
                    // Scroll to follow the motion event
                    var deltaX = (int)(_mLastMotionX - x);
                    _mLastMotionX = x;
                    int scrollX = ScrollX;


                    if (deltaX < 0)
                    {
                        if (scrollX > 0)
                        {
                            ScrollBy(Math.Max(-scrollX, deltaX), 0);
                        }
                    }
                    else if (deltaX > 0)
                    {
                        int availableToScroll =
                            GetChildAt(ChildCount - 1).Right - scrollX - Width;


                        if (availableToScroll > 0)
                        {
                            ScrollBy(Math.Min(availableToScroll, deltaX), 0);
                        }
                    }
                }


                break;


            case MotionEventActions.Up:
                if (_mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING)
                {
                    VelocityTracker velocityTracker = _mVelocityTracker;
                    velocityTracker.ComputeCurrentVelocity(VELOCITY_UNIT_PIXELS_PER_SECOND,
                                                           _mMaximumVelocity);
                    var velocityX = (int)velocityTracker.XVelocity;


                    if (velocityX > mDensityAdjustedSnapVelocity && _mCurrentScreen > 0)
                    {
                        // Fling hard enough to move left
                        SnapToScreen(_mCurrentScreen - 1);
                    }
                    else if (velocityX < -mDensityAdjustedSnapVelocity &&
                             _mCurrentScreen < ChildCount - 1)
                    {
                        // Fling hard enough to move right
                        SnapToScreen(_mCurrentScreen + 1);
                    }
                    else
                    {
                        SnapToDestination();
                    }


                    if (_mVelocityTracker != null)
                    {
                        _mVelocityTracker.Recycle();
                        _mVelocityTracker = null;
                    }
                }


                _mTouchState = TOUCH_STATE_REST;


                break;

            case MotionEventActions.Cancel:
                _mTouchState = TOUCH_STATE_REST;
                break;
            }


            return(true);
        }
Exemple #13
0
        public override bool OnTouchEvent(MotionEvent ev)
        {
            if (ev.Action == MotionEventActions.Down && ev.EdgeFlags != 0)
            {
                // Don't handle edge touches immediately -- they may actually belong to one of our
                // descendants.
                return(false);
            }

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

            if (mVelocityTracker == null)
            {
                mVelocityTracker = VelocityTracker.Obtain();
            }
            mVelocityTracker.AddMovement(ev);

            MotionEventActions action = ev.Action;
            float y = ev.GetY();
            float x = ev.GetX();

            switch (action)
            {
            case MotionEventActions.Down:
                /*
                 * If being flinged and user touches, stop the fling. isFinished
                 * will be false if being flinged.
                 */
                if (!mScroller.IsFinished)
                {
                    mScroller.AbortAnimation();
                }

                // Remember where the motion event started
                mLastMotionY = y;
                mLastMotionX = x;
                break;

            case MotionEventActions.Move:
                // Scroll to follow the motion event
                int deltaX = (int)(mLastMotionX - x);
                int deltaY = (int)(mLastMotionY - y);
                mLastMotionX = x;
                mLastMotionY = y;

                if (deltaX < 0)
                {
                    if (ScrollX < 0)
                    {
                        deltaX = 0;
                    }
                }
                else if (deltaX > 0)
                {
                    int rightEdge         = Width - PaddingRight;
                    int availableToScroll = GetChildAt(0).Right - ScrollX - rightEdge;
                    if (availableToScroll > 0)
                    {
                        deltaX = System.Math.Min(availableToScroll, deltaX);
                    }
                    else
                    {
                        deltaX = 0;
                    }
                }
                if (deltaY < 0)
                {
                    if (ScrollY < 0)
                    {
                        deltaY = 0;
                    }
                }
                else if (deltaY > 0)
                {
                    int bottomEdge        = Height - PaddingBottom;
                    int availableToScroll = GetChildAt(0).Bottom - ScrollY - bottomEdge;
                    if (availableToScroll > 0)
                    {
                        deltaY = System.Math.Min(availableToScroll, deltaY);
                    }
                    else
                    {
                        deltaY = 0;
                    }
                }
                if (deltaY != 0 || deltaX != 0)
                {
                    ScrollBy(deltaX, deltaY);
                }
                break;

            case MotionEventActions.Up:
                VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.ComputeCurrentVelocity(1000, mMaximumVelocity);
                int initialXVelocity = (int)velocityTracker.XVelocity;
                int initialYVelocity = (int)velocityTracker.YVelocity;
                if ((System.Math.Abs(initialXVelocity) + System.Math.Abs(initialYVelocity) > mMinimumVelocity) && ChildCount > 0)
                {
                    Fling(-initialXVelocity, -initialYVelocity);
                }
                if (mVelocityTracker != null)
                {
                    mVelocityTracker.Recycle();
                    mVelocityTracker = null;
                }
                break;
            }
            return(true);
        }
Exemple #14
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            if (mVelocityTracker == null)
            {
                mVelocityTracker = VelocityTracker.Obtain();
            }
            mVelocityTracker.AddMovement(e);
            MotionEventActions action = e.Action;

            if (action == MotionEventActions.Down)
            {
                x = e.GetX();

                if (IsSlided())
                {
                    dispatched = DispatchTouchEventToView(GetChildAt(0), e);
                }
                else
                {
                    dispatched = DispatchTouchEventToView(GetChildAt(1), e);
                }
            }
            else if (action == MotionEventActions.Move)
            {
                if (dispatched)
                {
                    if (IsSlided())
                    {
                        DispatchTouchEventToView(GetChildAt(0), e);
                    }
                    else
                    {
                        DispatchTouchEventToView(GetChildAt(1), e);
                    }
                }
                else
                {
                    float dx   = e.GetX() - x;
                    View  view = this.GetChildAt(1);
                    int   left = (int)(view.Left + dx);
                    if (left >= 0)
                    {
                        view.Layout(left, view.Top, view.Width + left,
                                    view.Top + view.Height);
                    }
                }
                x = e.GetX();
            }
            else if (action == MotionEventActions.Cancel ||
                     action == MotionEventActions.Up)
            {
                if (dispatched)
                {
                    if (IsSlided())
                    {
                        DispatchTouchEventToView(GetChildAt(0), e);
                    }
                    else
                    {
                        DispatchTouchEventToView(GetChildAt(1), e);
                    }
                }
                else
                {
                    mVelocityTracker.ComputeCurrentVelocity(1000);
                    int velocityX = (int)mVelocityTracker.GetXVelocity(0);
                    if (velocityX > VELOCITY_X_SPEED)
                    {
                        SetSlided(true);
                    }
                    else if (velocityX < -VELOCITY_X_SPEED)
                    {
                        SetSlided(false);
                    }
                    else
                    {
                        View view = GetChildAt(1);
                        if (view.Left >= view.Width / 2)
                        {
                            SetSlided(true);
                        }
                        else
                        {
                            SetSlided(false);
                        }
                    }
                    if (mVelocityTracker != null)
                    {
                        try
                        {
                            mVelocityTracker.Recycle();
                        }
                        catch
                        { }
                    }
                }
            }
            else if (!IsSlided())
            {
                DispatchTouchEventToView(GetChildAt(1), e);
            }
            return(true);
        }