I wonder how it worked for others, it does not work for me.
onInterceptTouchEvent() accepts only the ACTION_DOWN event. So far, onTouchEvent() accepts only one event at a time: ACTION_DOWN , ACTION_UP and others.
I had to override both onInterceptTouchEvent() and onTouchEvent() to make it work correctly. ACTION_DOWN of onInterceptTouchEvent captures the starting point of X , and OnTouchEvent captures the ending point of X2 . Which I compared it to determine the direction of movement.
Get the initial value of X touch:
float x1 = 0; @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch(ev.getAction() & MotionEventCompat.ACTION_MASK){ case MotionEvent.ACTION_DOWN: x1 = ev.getX(); break; } return super.onInterceptTouchEvent(ev); }
From the tjlian616 code, onTouchEvent() listens for ACTION_UP and gets its last X value. So far, I have established that the current value of the element is 0 to get swipe in the start listening. Compare it and add an activated listener.
@Override public boolean onTouchEvent(MotionEvent ev){ if(getCurrentItem()==0){ final int action = ev.getAction(); switch(action & MotionEventCompat.ACTION_MASK){ case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: mStartDragX = ev.getX(); if (x1<mStartDragX){ Log.i("TOUCH: ", "ACTION UP " + x1 + " : " + mStartDragX ); mListener.onSwipeOutAtStart(); }else{ Log.i("TOUCH ELSE : ", "ACTION UP " + x1 + " : " + mStartDragX ); mStartDragX = 0; } break; } }else{ mStartDragX=0; } return super.onTouchEvent(ev); }
user936597 Sep 04 '14 at 3:40 2014-09-04 03:40
source share