vendredi 1 juillet 2016

How to implement swipe down gesture on android correctly

I want to do something on swipe down on my view:

Swipe down from the calendar strip.

enter image description here

Calendar strip is is view extended from RecylerView and every item in it (week) is custom view.

I use onTouch and GestureDetectorCompat in my fragment to implement swipe, but it often doedn't detected.

//Calendar strip on touch event
@Override
public boolean onTouch(View v, MotionEvent event)
{
    gesture_detector.onTouchEvent(event);
    float x = event.getX();
    float y = event.getY();

    if(event.getAction()==MotionEvent.ACTION_DOWN)
    {
        old_x=x;
        old_y=y;
        Log.d("Touch","DOWN");
    }

        float dX = x - old_x;
        float dY = y - old_y;

        Log.d("Touch","dX = "+dX+" dY = "+dY);

        if((dX==0 && dY == 0) || (2*Math.abs(dY)<Math.abs(dX)))
        {
            dayPickerView.onTouchEvent(event);
        }

    old_x = x;
    old_y = y;


    return true;
}

...

//GestureDetector - onFling
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
    float y_2=0;
    int strip_height = dayPickerView.getHeight();

    if(e2!=null)
        y_2=e2.getY();

    if((velocityY>100)&&(y_2>2*strip_height))
    {
       //Do what we need
    }

    return true;
}

The main problem (?) is I need to get touch event in fragment and child calendar strip at the same time.

If I return true in onTouch of my child, then it doesn't fire in fragment. If I don't do this, I won't get move, up touch events, only down.

Aucun commentaire:

Enregistrer un commentaire