I know this is an old question, but I was just looking for a solution and came across this link (which oddly references this question). Anyway, I was able to figure out a solution based on their comments. The basic idea is to allow touch events based on the state of a boolean flag which you set.
Extend ViewPager on override onInterceptTouchEvent & onTouchEvent to only call super if you've set the flag. My class looks like this:
public class MyViewPager extends ViewPager { private boolean isPagingEnabled = false; public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } public MyViewPager(Context context) { super(context); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (isPagingEnabled) { return super.onInterceptTouchEvent(event); } return false; } @Override public boolean onTouchEvent(MotionEvent event) { if (isPagingEnabled) { return super.onTouchEvent(event); } return false; } public void setPagingEnabled(boolean pagingEnabled) { isPagingEnabled = pagingEnabled; } }
In your layout xml, replace your com.android.support.v4.ViewPager elements with com.yourpackage.MyViewPager elements.
Since you return 0.3f from getPageWidth() in your pager adapter, you would want scrolling enabled when the fourth item has been added to it. The tricky part is having this line of code everywhere when you define your pager adapter, and add or remove any objects from the adapter backing list.
mPager.setPagingEnabled(items.size() > 3);