android - Swipe run by Activity Do OnBackPress -


it can capture left swipe action right , run command activity? in case of activity identify swipe (left right) , run command onbackpress(); need identify swipe @ view , hold other components in activity , not know how identify whether action activity or component.

with following code can have listener responds gesture. possible view setontouchlistener have ability identify movement , perform action.

execution

        android.view.gesturedetector gesturedetector = new android.view.gesturedetector(this, new gesturedetector(activity.this));         view.ontouchlistener gesturelistener = new view.ontouchlistener() {             public boolean ontouch(view v, motionevent event) {                 return gesturedetector.ontouchevent(event);             }         };          view.setontouchlistener(gesturelistener); 

gesturedetector.java

public class gesturedetector extends android.view.gesturedetector.simpleongesturelistener {      private static final int swipe_min_distance = 120;     private static final int swipe_max_off_path = 250;     private static final int swipe_threshold_velocity = 200;     private final activity activity;      public gesturedetector(activity activity){         this.activity = activity;     }      @override     public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {         try {             if (math.abs(e1.gety() - e2.gety()) > swipe_max_off_path)                 return false;             if (e2.getx() - e1.getx() > swipe_min_distance && math.abs(velocityx) > swipe_threshold_velocity) {                 activity.onbackpressed();             }         } catch (exception e) {             // nothing         }         return false;     }      @override     public boolean ondown(motionevent e) {         return true;     } } 

Comments