Detect gesture gesture when there is ScrollView Android

I need to detect when there is scrolling in my application, I used this code and it works great:

private float x1,x2;
static final int MIN_DISTANCE = 150;

      

and override the onTouchEvent () method:

@Override
 public boolean onTouchEvent(MotionEvent event)
 {     
     switch(event.getAction())
     {
       case MotionEvent.ACTION_DOWN:
           x1 = event.getX();                         
       break;         
       case MotionEvent.ACTION_UP:
           x2 = event.getX();
           float deltaX = x2 - x1;
           if (Math.abs(deltaX) > MIN_DISTANCE)
           {
             Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show ();
           }
           else
           {
               // consider as something else - a screen tap for example
           }                          
       break;   
     }           
     return super.onTouchEvent(event);       
 }

      

But if I have a scrollView in my work, the code doesn't work anymore, how can I fix it? Do I need to completely change the code I am using?

EDIT: I tried to add the following method inside an if that defines swipe gestures:

if (getParent() != null) {
                   getParent().requestDisallowInterceptTouchEvent(true);
               }

      

But I am getting the error

requestDisallowInterceptTouchEvent

It says that I need to add cast to getParent ()

+3


source to share


1 answer


yes, you can fix it :-) And there are 3 things you need to do:

  • You need to add this method to your activity, this way you make sure that your function onTouchEvent

    always catches the event:

    @Override
    public boolean dispatchTouchEvent(MotionEvent event){
        this.onTouchEvent(event);
        return super.dispatchTouchEvent(event);
    }
    
          

  • Add a global boolean variable as a flag. This is because when there is a ListView the super.dispatchTouchEvent allows the event to be consumed ListView

    . However, when not ListView

    , the above code will send the same validation event onTouchEvent

    twice (second time through super.dispatchTouchEvent

    ):

    boolean swapped = false;

  • change the onTouchEvent function to use the changed flags:

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {  
     if(swapped){
         /*Make sure you don't swap twice,
    since the dispatchTouchEvent might dispatch your touch event to this function again!*/
         swapped = false;
         return super.onTouchEvent(event);
     }
     switch(event.getAction())
     {
       case MotionEvent.ACTION_DOWN:
           x1 = event.getX();                         
       break;         
       case MotionEvent.ACTION_UP:
           x2 = event.getX();
           float deltaX = x2 - x1;
           if (Math.abs(deltaX) > MIN_DISTANCE)
           {
             Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show();
             //you already swapped, set flag swapped = true
             swapped = true;
           }
           else
           {
               // not swapping
           }                          
       break;   
     }           
     return super.onTouchEvent(event);       
    
          

    }



Note. Don't add the code mentioned in your post and your MIN_DISTANCE is a little small, set it to 250.

0


source







All Articles