0

I'm developing swipe left and right in a custom renderer for android in a xamarin forms project. I have a class that implements an override of method "DispatchTouchEvent(MotionEvent touch)". When MotionEventActions.Up is raised, I do some things. I have implemented a "ItemSelected" event in shared code for my ListView but when I swipe on a element of the listview, it interprets that is a tap and this trigger this event.

How can I differentiate when I tap or swipe in an element?

I have been thinking in differenciate when I swipe in "MotionEventActions.Up" but I don't know how to show a new page from a custom renderer.

This is the code of the renderer:

class SwipeListRenderer : ListViewRenderer
{
    bool swipe = false;
    bool scroll;
    double puntoYinicial = 0.0;

    public override bool DispatchTouchEvent(MotionEvent touch)
    {
        try
        {
            if (puntoYinicial == 0.0)
                puntoYinicial = touch.GetY();

            double currentQuota = ((touch.GetX() - TouchDispatcher.StartingBiasX) / (double)this.Width);
            float x = touch.GetX();
            float y = touch.GetY();
            SwipeList.SwipeItemView touchedElement = (TouchDispatcher.TouchingView as SwipeList.SwipeItemView);
            switch (touch.ActionMasked)
            {

                case MotionEventActions.Down:
                    (this.Element as SwipeList.SwipeListView).RestoreItemPosition();

                    swipe = false;
                    scroll = false;

                    return base.DispatchTouchEvent(touch);
                    break;

                case MotionEventActions.Up:
                    if (swipe)
                        touchedElement.CompleteTranslation(currentQuota);      

                    (this.Element as SwipeList.SwipeListView).AppendTouchedElement(touchedElement);

                    TouchDispatcher.TouchingView = null;
                    TouchDispatcher.StartingBiasX = 0;
                    TouchDispatcher.StartingBiasY = 0;
                    puntoYinicial = 0.0;
                    break;
                case MotionEventActions.Move:
                    if (touchedElement != null)
                        TouchDispatcher.TouchingView.PerformTranslation(currentQuota);

                    break;
            }
            return base.DispatchTouchEvent(touch);
        }
        catch (Exception ex)
        {

            throw;
        }
    }
}
ivanbm
  • 5
  • 2

1 Answers1

0

I have had a similar problem and this is how I fixed it. Please see my answer for this stackoverflow question. In this answer, in the code behind code, you can see how I used TapGestureRecognizer. Hope it helps.

Community
  • 1
  • 1
Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20