Android view with multi-directional swipe gestures -


i have view textview containing long text, view has give user facility of scrolling through text.

also, textview must allow swipe right , left on instances of view.

i've got scroll working scollingmovementmethod left , right swipe doesn't work.

for enabling swipe gesture left,right,up or down, follow below steps:

step 1: create java file simplegesturefilter

import android.app.activity; import android.view.gesturedetector; import android.view.motionevent;  /** * created nihal on 1/21/2016. */ public class simplegesturefilter extends    gesturedetector.simpleongesturelistener { public final static int swipe_up= 1; public final static int swipe_down= 2; public final static int swipe_left= 3; public final static int swipe_right = 4; public final static int mode_transparent = 0; public final static int mode_solid= 1; public final static int mode_dynamic= 2; private final static int action_fake = -13; //just unlikely number private int swipe_min_distance = 100; private int swipe_max_distance = 350; private int swipe_min_velocity = 100; private int mode = mode_dynamic; private boolean running = true; private boolean tapindicator = false; private activity context; private gesturedetector detector; private simplegesturelistener listener; public simplegesturefilter(activity context,simplegesturelistener sgl) {      this.context = context;     this.detector = new gesturedetector(context, this);     this.listener = sgl; } public void ontouchevent(motionevent event){      if(!this.running)         return;      boolean result = this.detector.ontouchevent(event);      if(this.mode == mode_solid)         event.setaction(motionevent.action_cancel);     else if (this.mode == mode_dynamic) {          if(event.getaction() == action_fake)             event.setaction(motionevent.action_up);         else if (result)             event.setaction(motionevent.action_cancel);         else if(this.tapindicator){             event.setaction(motionevent.action_down);             this.tapindicator = false;         }      }     //else nothing, it's transparent } public void setmode(int m){     this.mode = m; } public int getmode(){     return this.mode; } public void setenabled(boolean status){     this.running = status; } 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;  @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;         }         // right left swipe         if (e1.getx() - e2.getx() > swipe_min_distance                 && math.abs(velocityx) > swipe_threshold_velocity) { // right left             this.listener.onswipe(swipe_left);            }if (e2.getx() - e1.getx() > swipe_min_distance                 && math.abs(velocityx) > swipe_threshold_velocity) { // right left             this.listener.onswipe(swipe_right);            } else if (e2.getx() - e1.getx() > swipe_min_distance                 && math.abs(velocityx) > swipe_threshold_velocity) { //    bottom             this.listener.onswipe(swipe_up);            }      } catch (exception e) {      }     return false; }  @override public boolean onsingletapup(motionevent e) {     this.tapindicator = true;     return false; } @override public boolean ondoubletap(motionevent arg0) {     this.listener.ondoubletap();;     return true; }  @override public boolean ondoubletapevent(motionevent arg0) {     return true; } @override public boolean onsingletapconfirmed(motionevent arg0) {      if(this.mode == mode_dynamic){// owe action_up, fake an               arg0.setaction(action_fake);//action converted action_up later.                                            this.context.dispatchtouchevent(arg0);     }     return false; }  public interface simplegesturelistener{     void onswipe(int direction);     void ondoubletap(); }  } 

step 2: extend appcompatactivity , implement simplegesturefilter.simplegesturelistener in activity this:

public class youractivity extends appcompatactivity implements      simplegesturefilter.simplegesturelistener {      //inside of activity, code here   } 

step 3: define

private simplegesturefilter detector; 

step4: in oncreate method add this:

detector = new simplegesturefilter(this,this); 

now youractivity should

public class youractivity extends appcompatactivity implements      simplegesturefilter.simplegesturelistener {  private simplegesturefilter detector;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.yourlayout);     detector = new simplegesturefilter(this,this);  }   // add codes swipe gesture here    } 

step 5: add codes swipe gesture in youractivity

public class sources extends appcompatactivity implements  simplegesturefilter.simplegesturelistener {  private simplegesturefilter detector;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.sources);     detector = new simplegesturefilter(this,this);  }    @override public boolean dispatchtouchevent(motionevent me){      this.detector.ontouchevent(me);      return super.dispatchtouchevent(me);  } @override  public void onswipe(int direction) {     string str = "";        switch (direction) {          case simplegesturefilter.swipe_right :              //**code** here wanna **right swipe**          case simplegesturefilter.swipe_left :            //**code** here wanna **left swipe**           case simplegesturefilter.swipe_down :              break;          case simplegesturefilter.swipe_up :              break;      }    }     @override  public void ondoubletap() {      toast.maketext(this, "try swiping left", toast.length_long).show();  } 

follow above steps correctly, able make swipe gestures work


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -