在一定的方向上的ViewPage停止刷卡 [英] Viewpage stop swiping in a certain direction
本文介绍了在一定的方向上的ViewPage停止刷卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能阻止viewpager从滚动只在一个方向。例如,允许刷卡只有在正确的,而不是左侧。我绝对坚持,任何帮助将大大AP preciated。需要注意的是,我需要得到这个工作为Android 2.2版本,所以我现在用的相容性库ViewPager。
How can I stop viewpager from scrolling in one direction only. For example, allow swipes only to the right, but not to the left. I am absolutely stuck, any help would be greatly appreciated. To note that I need to get this to work for Android version 2.2, so I am using the compatibilty library for ViewPager.
在此先感谢
推荐答案
您将不得不作出自己的ViewPager,它扩展了原有的ViewPager和覆盖的onTouchEvent方法
You will have to make your own ViewPager that extends the original ViewPager and override the onTouchEvent method
public class CustomViewPager extends ViewPager {
float lastX = 0;
boolean lockScroll = false;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewPager(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = ev.getX();
lockScroll = false;
return super.onTouchEvent(ev);
case MotionEvent.ACTION_MOVE:
if (lastX > ev.getX()) {
lockScroll = false;
} else {
lockScroll = true;
}
lastX = ev.getX();
break;
}
lastX = ev.getX();
if(lockScroll) {
return false;
} else {
return super.onTouchEvent(ev);
}
}
}
这篇关于在一定的方向上的ViewPage停止刷卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文