java - Android - swipe/onLongClick conflict -
i have textview swipe listener on it. wanted add long click listener well. both work when swipe, long click listener triggered well, not want. need them independent. i've found very old code detecting long press it's not clear what's best approach.
here's have in mainactivity
tvita.setontouchlistener(new onswipetouchlistener(mainactivity.this) { @override public void onswiperight() {changesays(); } @override public void onswipeleft() { changesays(); } }); tvita.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { string label = "label_ita"; string text = tvita.gettext().tostring(); clipboardmanager clipboard = (clipboardmanager) getsystemservice(mainactivity.this.clipboard_service); clipdata clip = clipdata.newplaintext(label, text); clipboard.setprimaryclip(clip); toast.maketext(mainactivity.this, "copied clipboard!", toast.length_short).show(); return true; } });
this swipe listener class
public class onswipetouchlistener implements view.ontouchlistener { private final gesturedetector gesturedetector; public onswipetouchlistener(context context) { gesturedetector = new gesturedetector(context, new gesturelistener()); } public void onswipeleft() { } public void onswiperight() { } public boolean ontouch(view v, motionevent event) { return gesturedetector.ontouchevent(event); } private final class gesturelistener extends gesturedetector.simpleongesturelistener { private static final int swipe_distance_threshold = 100; private static final int swipe_velocity_threshold = 100; // commented out otherwise won't work // @override // public boolean ondown(motionevent e) { // return true; // } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { float distancex = e2.getx() - e1.getx(); float distancey = e2.gety() - e1.gety(); if (math.abs(distancex) > math.abs(distancey) && math.abs(distancex) > swipe_distance_threshold && math.abs(velocityx) > swipe_velocity_threshold) { if (distancex > 0) onswiperight(); else onswipeleft(); return true; } return false; } } }
thank you!
try synchronize both events using booleans. following code example:
boolean swiped = false; boolean clicked = false; tvita.setontouchlistener(new onswipetouchlistener(mainactivity.this) { @override public void onswiperight() { if(!clicked) { swiped = true; changesays(); } } @override public void onswipeleft() { if(!clicked) { swiped = true; changesays(); } } @override public boolean ontouch(view v, motionevent event) { if(event.getaction() == motionevent.action_up) { swiped = false; clicked = false; } return super(v, event); } }); tvita.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { if(!swiped) { clicked = true; string label = "label_ita"; string text = tvita.gettext().tostring(); clipboardmanager clipboard = (clipboardmanager) getsystemservice(mainactivity.this.clipboard_service); clipdata clip = clipdata.newplaintext(label, text); clipboard.setprimaryclip(clip); toast.maketext(mainactivity.this, "copied clipboard!", toast.length_short).show(); } return true; } });
Comments
Post a Comment