问题
I am trying to use ViewPager for showing different views.
Below is code I am using to check whether swiping is left or right
myPager.setOnTouchListener(new ViewPager.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
if (event.getX() > mStartDragX ) {
Log.i(TAG,"SWIPING RIGHT");
} else if (event.getX() < mStartDragX) {
Log.i(TAG,"SWIPING LEFT");
}
break;
}
return false;
}
});
So when I start to swipe from right to left or right to left its showing me correct log message.
But if I started to swipe from right to left and at the same time again if start to swipe from left to right, how can I identify whether currently swiping in which direction?
Can anyone please help ?
Thanks
回答1:
Then do somethink like this..
case MotionEvent.ACTION_MOVE:
if (event.getX() > mStartDragX) {
Log.i(TAG, "SWIPING RIGHT");
mStartDragX = event.getX();
} else if (event.getX() < mStartDragX) {
Log.i(TAG, "SWIPING LEFT");
mStartDragX = event.getX();
}
Here i am saving the current position again to old position so whenever the swiped back it will give the correct result..
来源:https://stackoverflow.com/questions/22680978/how-to-get-current-swipe-direction-in-action-in-viewpager