0

I have 2 view pagers in a linear layout both taking up same amount of screen. I want to make it so that if a user swipes any where on the screen then the swipe should only be for the lower viewpager. I tried increasing the touch delegate but that didn't work.

MikeC
  • 255
  • 4
  • 16
  • If you want the swipe to work only for the 2nd viewpager why do you even have the first viewpager in place? – Antrromet Jul 02 '15 at 21:55
  • The 2nd viewpager programmatically swipes the 1st viewpager to give it a delay effect. – MikeC Jul 02 '15 at 22:00

1 Answers1

2

Well you're trying to some trippy stuff! I'd suggest making a CustomViewPager class for the 2nd viewpager which can have an instance of the 1st viewPager.

    public class CustomViewPager extends ViewPager {

    private ViewPager firstViewPager;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(firstViewPager!=null){
            firstViewPager.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if(firstViewPager!=null){
            firstViewPager.onInterceptTouchEvent(event);
        }
        return super.onInterceptTouchEvent(event);
    }

    public void setFirstViewPager(ViewPager firstViewPager) {
        this.firstViewPager = firstViewPager;
    } 
}

In this you are passing the motion event that you get in the 2nd viewpager's onTouchEvent and onInterceptTouchEvent to the 1st viewpager. You'd also want to disable the swipe on the 1st viewpager that you could do as described here.

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • And since you mentioned `touch delegate` am assuming you're from an iOS background :) Good luck with your Android app! – Antrromet Jul 02 '15 at 22:16