Disable swipe in fragmentPagerAdapter? - android

You can setup a custom view pager like this :-

public class LockableViewPager extends ViewPager {
    private boolean swipeable;

    public LockableViewPager(Context context) {
        super(context);    
    }    

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

    @Override    
    public boolean onTouchEvent(MotionEvent event) {    
        if (this.swipeable) {    
            return super.onTouchEvent(event);    
        }    
        return false;    
    }

    @Override

    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.swipeable) {
            return super.onInterceptTouchEvent(event);
        }
        return false;
    }

    public void setSwipeable(boolean swipeable) {
        this.swipeable = swipeable;
    }
}

And then in the layout you can set it as :-

<mypackage.lockableviewpager 
    android:id="@+id/photosViewPager" 
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

And then just call

setSwipeable(false)

Hope it helps.


Here's a little more elaboration on the LockableViewPager class example...

First instead of marking up a ViewPager like this

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

you want to make a custom class that inherits VP like this

public class LockableViewPager extends ViewPager {
  private boolean swipeable;

  public LockableViewPager(Context context) {
      super(context);
  }

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

  @Override
  public boolean onTouchEvent(MotionEvent event) {
      if (this.swipeable) {
          return super.onTouchEvent(event);
      }
      return false;
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
      if (this.swipeable) {
          return super.onInterceptTouchEvent(event);
      }
      return false;
  }

  public void setSwipeable(boolean swipeable) {
      this.swipeable = swipeable;
  }
}

Pay attention to this method

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
  if (this.swipeable) {
      return super.onInterceptTouchEvent(event);
  }
  return false;
}

then you can mark up your custom class that already acts as ViewPager plus intercepts touch events

<com.YourPackage.LockableViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.YourPackage.LockableViewPager>

In your activity's java class that contains LockableViewPager set

LockableViewPager viewPager;
OnCreate() {
  viewPager = (LockableVewPager) findViewById(R.id.*id of pager*);
  viewPager.setSwipable(false);
}

Extendng the class you want (i.e. ViewPager) is the CORRECT way to do this unless you are making a totally different class that doesn't behave the same way ;* good luck