android - Issue trying to detect swipe to change views in ViewFlipper -
i'm trying implement swipe detection change views in viewflipper
. i've used code shown below, copied there.
while ontouch
triggered when touching viewflipper
, onfling event never triggered.
anybody can help? what's best way detect swipe?
thanks
public class homeactivity extends activity { viewflipper viewflipper; private gesturedetector gesturedetector; private animation slideleftin, slideleftout, sliderightin, sliderightout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.home); linearlayout home = (linearlayout) findviewbyid(r.id.home); // gesture detection gesturedetector = new gesturedetector(new mygesturedetector()); // info popup imageview btn_open_popup = (imageview) findviewbyid(r.id.btn_popup); slideleftin = animationutils.loadanimation(this, r.anim.slide_left_in); slideleftout = animationutils.loadanimation(this, r.anim.slide_left_out); sliderightin = animationutils.loadanimation(this, r.anim.slide_right_in); sliderightout = animationutils.loadanimation(this, r.anim.slide_right_out); // compute size according density float density = getresources().getdisplaymetrics().density; final int width = (int) (295.0f * density); final int height = (int) (307.0f * density); btn_open_popup.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { layoutinflater inflater = (layoutinflater) homeactivity.this.getsystemservice(context.layout_inflater_service); view popup_container = inflater.inflate(r.layout.popup_container,null, false); final popupwindow pw = new popupwindow(popup_container, width, height, true); pw.showatlocation(findviewbyid(r.id.home), gravity.center, 0,0); imageview btn_close_popup = (imageview) popup_container.findviewbyid(r.id.btn_close_popup); btn_close_popup.setalpha(120); btn_close_popup.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { pw.dismiss(); } }); // set swipe listener viewflipper viewflipper=(viewflipper)popup_container.findviewbyid(r.id.popup_flipper); viewflipper.setontouchlistener(new ontouchlistener() { public boolean ontouch(view v, motionevent event) { if (gesturedetector.ontouchevent(event)) { return true; } else { return false; } } }); } }); } class mygesturedetector extends 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; public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { system.out.println(" in onfling() :: "); if (math.abs(e1.gety() - e2.gety()) > swipe_max_off_path) return false; if (e1.getx() - e2.getx() > swipe_min_distance && math.abs(velocityx) > swipe_threshold_velocity) { viewflipper.setinanimation(sliderightin); viewflipper.setoutanimation(slideleftout); viewflipper.shownext(); } else if (e2.getx() - e1.getx() > swipe_min_distance && math.abs(velocityx) > swipe_threshold_velocity) { viewflipper.setinanimation(slideleftin); viewflipper.setoutanimation(sliderightout); viewflipper.showprevious(); } return super.onfling(e1, e2, velocityx, velocityy); } } }
Comments
Post a Comment