android - Using GestureDetector and clickable RelativeLayout -
i have problem using gesturedetector , clickable relativelayout @ same time. when relativelayout not clickable app recognize every gesture perfectly, when set layout clickable app doesn't recoginze gestures. there way use both @ same time?
unfortunately, onclick() & ontouch() cannot behave when used together. use work-around detect click using ontouch. not sure how behave gesture detector present. share code.. works fine without gesture detector. let me know if it's not working.
initializing listeners :
gesturedetector gdt; view.ontouchlistener touchlistener; private void initializelistener() { gdt = new gesturedetector(mcontext,new gesturelistener()); touchlistener = new view.ontouchlistener() { float startx; float starty; float click_action_threshold = 5; //uses touched coordinates recorded through motion_down & motion_up detect whether action performed click private boolean isaclick(float startx, float endx, float starty, float endy) { if(isloadingview) return false; float differencex = math.abs(startx - endx); float differencey = math.abs(starty - endy); return !(differencex > click_action_threshold || differencey > click_action_threshold); } @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: startx = event.getx(); starty = event.gety(); break; case motionevent.action_up: { float endx = event.getx(); float endy = event.gety(); if (isaclick(startx, endx, starty, endy)) { switch (v.getid()) { case r.id.view_id: break } } } break; } gdt.ontouchevent(event); return true; } }; }
add listener views :
private void addtouchlistenertoview(view v) { v.setontouchlistener(touchlistener); }
sample gesture listener class:
private class gesturelistener extends gesturedetector.simpleongesturelistener { private final int swipe_min_distance = 153; private final int swipe_threshold_velocity = 20; @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { if(e1.getx() - e2.getx() > swipe_min_distance && math.abs(velocityx) > swipe_threshold_velocity) { return false; // right left } else if (e2.getx() - e1.getx() > swipe_min_distance ) //&& math.abs(velocityx) > swipe_threshold_velocity) { return false; // left right } if(e1.gety() - e2.gety() > swipe_min_distance)// && math.abs(velocityy) > swipe_threshold_velocity) { return false; // bottom top } else if (e2.gety() - e1.gety() > swipe_min_distance)// && math.abs(velocityy) > swipe_threshold_velocity) { return false; // top bottom } return true; } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { if(e1.getx() - e2.getx() > swipe_min_distance*10 ) //&& math.abs(velocityx) > swipe_threshold_velocity) { return false; // right left } else if (e2.getx() - e1.getx() > swipe_min_distance*10 ) //&& math.abs(velocityx) > swipe_threshold_velocity) { return false; // left right } return true; } }
Comments
Post a Comment