Android handeling swipe gesture on an edittext -
i'm trying swipe gesture work on edittext. try tends buggy.
when return false gesture works cursor moves when making swipe gesture. when return result gesturelistener (onfling) cursor stay's in place, on android 2.3.3 after gesture finished context menu text edit pops up, on 4.1.2 there no context menu, making gesture select whole word.
here current code:
package com.example.testmarkup; import android.content.context; import android.view.gesturedetector; import android.view.gesturedetector.simpleongesturelistener; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; public class onswipetouchlistener implements ontouchlistener { public interface onswipelistener { public void onswipe(); } private gesturedetector mgesturedetector = null; private onswipelistener monswipeleftlistener; private onswipelistener monswiperightlistener; private gesturelistener mgesturelistener = null; public onswipetouchlistener(final context context) { mgesturelistener = new gesturelistener(); mgesturedetector = new gesturedetector(context, mgesturelistener); } public boolean ontouch(final view view, final motionevent motionevent) { final boolean result = mgesturedetector.ontouchevent(motionevent); //when return false here gesture works cursor moves when making swipe gesture. //however when return result gesturelistener (onfling) cursor stay's in place, //but on android 2.3.3 after gesture finished context menu text edit pops up, //on 4.1.2 there no context menu, making gesture select whole word. // return false; return result; } private final class gesturelistener extends simpleongesturelistener { private static final int swipe_threshold = 100; private static final int swipe_velocity_threshold = 100; @override public boolean onfling(final motionevent e1, final motionevent e2, final float velocityx, final float velocityy) { boolean result = false; try { final float diffy = e2.gety() - e1.gety(); final float diffx = e2.getx() - e1.getx(); if (math.abs(diffx) > math.abs(diffy)) { if (math.abs(diffx) > swipe_threshold && math.abs(velocityx) > swipe_velocity_threshold) { if (diffx > 0) { onswiperight(); result = true; } else { onswipeleft(); result = true; } } } else { if (math.abs(diffy) > swipe_threshold && math.abs(velocityy) > swipe_velocity_threshold) { if (diffy > 0) { onswipebottom(); result = true; } else { onswipetop(); result = true; } } } } catch (final exception exception) { exception.printstacktrace(); } return result; } } public void onswiperight() { if (monswiperightlistener != null) monswiperightlistener.onswipe(); } public void onswipeleft() { if (monswipeleftlistener != null) monswipeleftlistener.onswipe(); } public void onswipetop() { } public void onswipebottom() { } public onswipetouchlistener setonswipeleft(final onswipelistener alistener) { monswipeleftlistener = alistener; return this; } public onswipetouchlistener setonswiperight(final onswipelistener alistener) { monswiperightlistener = alistener; return this; } }
you can use droidquery handle swipe events. if follow link, there lot of other questions how this. essentially, can this:
//global variables private boolean isswiping = false; private swipedetector.direction swipedirection = null; private view v;//set parent layout of fragments. //swipe-handling code $.with(v).swipe(new function() { @override public void invoke($ droidquery, object... params) { if (params[0] == swipedetector.direction.start) isswiping = true; else if (params[0] == swipedetector.direction.stop) { if (isswiping) { isswiping = false; if (swipedirection != null) { switch(swipedirection) { case down : //todo: down swipe complete, break; case : //todo: swipe complete, break; case left : //todo: left swipe complete, break; case right : //todo: right swipe complete, (such as): break; default : break; } } } } else { swipedirection = (swipedetector.direction) params[0]; } } });
you can droidquery http://bit.ly/droidquery.
Comments
Post a Comment