android - how to implement both ontouch and also onfling in a same listview? -
i have listview , implemented onclick , onfling.problem when fling(swipe left right), onclick event of listview getting executed.how overcome problem? how differentiate touch(tap) , fling(swipe) in listview?
listclicklistener = new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v,int position, long id) { //job of onclick listener } }; mcontactlist.setonitemclicklistener(listclicklistener); mcontactlist.setadapter(adapter); // gesture detection gesturedetector = new gesturedetector(new mygesturedetector(prosnos)); gesturelistener = new view.ontouchlistener() { public boolean ontouch(view v, motionevent event) { if (gesturedetector.ontouchevent(event)) { return true; } return false; } }; mcontactlist.setontouchlistener(gesturelistener); } public class mygesturedetector extends simpleongesturelistener { @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { // fling event return false; } }
p.s. possible? comment onclicklistener of listview , writing same logic in ontouchevent? still have no doubt onfling call ontouch. right?
pseudo code answer clarify above comments. how have mysimplegesturelistener's ontouch method called.
public class gestureexample extends activity { protected mygesturelistener mygesturelistener; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mygesturelistener = new mygesturelistener(this); // or if have created gesture detector. // mygesturelistener = new mygesturelistener(this, getexistinggesturedetector()); // example of setting listener. ontouchevent called on listener ((listview)findviewbyid(r.id.listview)).setontouchlistener(mygesturelistener); } @override public boolean ontouchevent(motionevent event) { // or implement in activity or component. when not assigning child component. return mygesturelistener.getdetector().ontouchevent(event); } class mygesturelistener extends simpleongesturelistener implements ontouchlistener { context context; gesturedetector gdetector; public mygesturelistener() { super(); } public mygesturelistener(context context) { this(context, null); } public mygesturelistener(context context, gesturedetector gdetector) { if(gdetector == null) gdetector = new gesturedetector(context, this); this.context = context; this.gdetector = gdetector; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { return super.onfling(e1, e2, velocityx, velocityy); } @override public boolean onsingletapconfirmed(motionevent e) { return super.onsingletapconfirmed(e); } public boolean ontouch(view v, motionevent event) { // within mygesturelistener class can manage event.getaction() codes. // note calling gesture detectors ontouchevent. , given we've set class gesturedetectors listener // onfling, onsingletap etc methods executed. return gdetector.ontouchevent(event); } public gesturedetector getdetector() { return gdetector; } } }
Comments
Post a Comment