如何获取当前的刷卡方向行动ViewPager [英] How to get current swipe direction in action in ViewPager
问题描述
我想使用ViewPager用于显示不同的看法。
I am trying to use ViewPager for showing different views.
下面是code我使用的刷卡检查是否向左或向右
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?
任何人都可以请帮助?
感谢
推荐答案
然后就somethink这样的..
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..
这篇关于如何获取当前的刷卡方向行动ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!