Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Created April 27, 2013 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slightfoot/5475083 to your computer and use it in GitHub Desktop.
Save slightfoot/5475083 to your computer and use it in GitHub Desktop.
This ScrollingViewPager lets its children determine if they want to scroll before the view pager chooses to scroll/switch pages. I also have a default override for multitouch so that we can can force the view pager to ignore the touch events if multiple pointers are in use on the down action.Helpful if you have a horizontally scrolling view in a…
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class ScrollingViewPager extends ViewPager
{
private boolean mOverrideMultiTouch = true;
public ScrollingViewPager(Context context)
{
super(context);
}
public ScrollingViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void setOverrideMultiTouch(boolean overrideMultiTouch)
{
mOverrideMultiTouch = overrideMultiTouch;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if(mOverrideMultiTouch){
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
if(action == MotionEvent.ACTION_DOWN && MotionEventCompat.getPointerCount(ev) > 1){
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y)
{
if(v instanceof CanScrollCompat){
return checkV && ((CanScrollCompat)v).canScrollHorizontally(-dx);
}
return super.canScroll(v, checkV, dx, x, y);
}
public interface CanScrollCompat
{
public boolean canScrollHorizontally(int direction);
public boolean canScrollVertically(int direction);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment