Reputation: 1603
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.
Thanks in advance
Upvotes: 5
Views: 5525
Reputation: 2317
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);
}
}
}
Upvotes: 7