android - onClickListener not letting onFling trigger -
i have both onclicklistener , onfling (using gesturedetector).
if click on button onclicklistener fires.
if fling on body of screen onfling fires.
however if start fling button, neither fire.
layout follows:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <button android:id="@+id/mybutton" android:layout_margin="50dp" android:layout_height="100dp" android:layout_width="match_parent" android:text="button"/> </linearlayout> and code looks like:
public class mylayout extends activity implements gesturedetector.ongesturelistener { private gesturedetector gdetector; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mylayout); gdetector = new gesturedetector(getbasecontext(), this); findviewbyid(r.id.mybutton).setonclicklistener(button_onclicklistener); } final view.onclicklistener button_onclicklistener = new view.onclicklistener() { public void onclick(final view buttonview) { toast.maketext(getapplicationcontext(), "button pressed", toast.length_long).show(); } }; @override public boolean ondown(motionevent motionevent) { return true; } @override public void onshowpress(motionevent motionevent) { } @override public boolean onsingletapup(motionevent motionevent) { return false; } @override public boolean onscroll(motionevent motionevent, motionevent motionevent2, float v, float v2) { return false; } @override public void onlongpress(motionevent motionevent) { } @override public boolean onfling(motionevent start, motionevent finish, float v, float v2) { if (start.getrawy() < finish.getrawy()) { toast.maketext(getapplicationcontext(), "fling detected", toast.length_long).show(); } return false; } @override public boolean ontouchevent(motionevent me) { log.i("touch", "ontouchevent"); return gdetector.ontouchevent(me); } } how can onfling run first?
i have looked @ other posts (e.g. onclick blocking onfling) not found suitable answer.
you should try using onintercepttouchevent() (or, maybe dispatchtouchevent(), @ point i'm not sure suit better) of button's parent (or higher in hierarchy) decide whether route touch button or gesture detector.
if motionevent's coordinates belong button's rect , action_down followed action_up shortly, it's click , should routed button, if receive action_move , movement continues extent know it's swipe - should handled gesture detector.
upd here's answer explains technique https://stackoverflow.com/a/3834952/375929
Comments
Post a Comment