private void ListViewOnOnOverscrolled(object sender, OverscrollListenerListViewEventArgs overscrollListenerListViewEventArgs) { if (overscrollListenerListViewEventArgs.DeltaY < 0) { _overscrolling = true; vT.ComputeCurrentVelocity(1); if (_header != null && (int)Math.Ceiling(_header.GetY()) != 0 && _historyEvent.Action == MotionEventActions.Up || _historyEvent.Action == MotionEventActions.Cancel) { long duration = (long)Math.Ceiling(Math.Abs(_header.Height) / vT.GetYVelocity(0)); _header.Animate().Y(0).SetDuration(duration).SetInterpolator(new DecelerateInterpolator(1.0f)).Start(); _listView.Animate().Y(_header.Height).SetDuration(duration).SetInterpolator(new DecelerateInterpolator(1.0f)).Start(); } } else { _overscrolling = 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 DispatchTouchEvent(MotionEvent e) { var index = e.ActionIndex; var action = e.ActionMasked; var pointerId = e.GetPointerId(index); switch (action & MotionEventActions.Mask) { case MotionEventActions.PointerDown: MultiTouchGesture?.Invoke(this, EventArgs.Empty); break; case MotionEventActions.Up: if ((DateTime.Now - _downTime).TotalMilliseconds < 200) { SingleTapGesture?.Invoke(this, EventArgs.Empty); } break; } switch (action) { case MotionEventActions.Down: _downTime = DateTime.Now; if (_velocityTracker == null) { _velocityTracker = VelocityTracker.Obtain(); } else { // Reset the velocity tracker back to its initial state. _velocityTracker.Clear(); } if (IfVelocityTrackerIsNull()) { return(true); } _velocityTracker.AddMovement(e); break; case MotionEventActions.Move: if (IfVelocityTrackerIsNull()) { return(true); } _velocityTracker.AddMovement(e); _velocityTracker.ComputeCurrentVelocity(Sensitivity); TryExportVelocity(_velocityTracker.GetXVelocity(pointerId), _velocityTracker.GetYVelocity(pointerId)); break; case MotionEventActions.Up: case MotionEventActions.Cancel: if (IfVelocityTrackerIsNull()) { return(true); } _velocityTracker.Recycle(); _velocityTracker = null; break; } return(true); }