I'm trying to make note app and have some problems : I have custom Edittext inside custom LinearLayout and trying to handle swipe on Edittext. I'm using this solution of swipe recognition OnSwipeTouchListener and it works perfect, until I tap on edittext (in witch case edittext doesn't get focus). I set OnSwipeTouchListener as edittext.ontouchlistener public class OnSwipeTouchListener implements View.OnTouchListener {
private GestureDetector gestureDetector; public OnSwipeTouchListener(Context c) { gestureDetector = new GestureDetector(c, new GestureListener()); } public boolean onTouch(final View view, final MotionEvent motionEvent) { return gestureDetector.onTouchEvent(motionEvent); } private final class GestureListener extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_THRESHOLD = 100; private static final int SWIPE_VELOCITY_THRESHOLD = 100; @Override public boolean onSingleTapConfirmed(MotionEvent e){ print(211212, "singleTap shemovida"); return false; } @Override public boolean onDown(MotionEvent e) { return true; } // Determines the fling velocity and then fires the appropriate swipe event accordingly @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; print(21512512, "fling"); try { float diffY = e2.getY() - e1.getY(); 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(); } else { onSwipeLeft(); } } } else { if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { onSwipeDown(); } else { onSwipeUp(); } } } } catch (Exception exception) { exception.printStackTrace(); } return result; } } static void print(int line, String st){ PRINT.print("ONSwipeTouchListener", line, st); } public void onSwipeRight() { } public void onSwipeLeft() { } public void onSwipeUp() { } public void onSwipeDown() { } and I set it to edittext
void addTextView(){ _myText = new CustomEditText(getContext()); LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); _myText.setLayoutParams(params); _myText.setId(getEditTextId()); addView(_myText); _myText.setOnTouchListener(new OnSwipeTouchListener(getContext()) { @Override public void onSwipeDown() { } @Override public void onSwipeLeft() { CustomEditTextWithCheckbox.this.onSwipeLeft(); } @Override public void onSwipeUp() { } @Override public void onSwipeRight() { CustomEditTextWithCheckbox.this.onSwipeRight(); } }); } but as I guess in OnSwipeTouchListener.OnTouch always returns true and so edittext does not get focus. I have also tryed to change onTouch
public boolean onTouch(final View view, final MotionEvent motionEvent) { gestureDetector.onTouchEvent(motionEvent); return false; } and it works, but still problem is that on swipe edittext gets focus when i want to swipe, And I understand why its working like that. But i need that if user does not swipe than on touch should return false, else true. Can anyone help