Skip to content

Instantly share code, notes, and snippets.

Last active September 9, 2015 11:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A Custom ViewPager which can been disable swipe.
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class NonSwipeableViewPager extends ViewPager {
private boolean swipeable;
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
/*Declare this styleable in attrs.xml
<declare-styleable name="NonSwipeableViewPager">
<attr name="swipeable" format="boolean"/>
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NonSwipeableViewPager);
swipeable = ta.getBoolean(R.styleable.NonSwipeableViewPager_swipeable, true);
public boolean onTouchEvent(MotionEvent event) {
return swipeable && super.onTouchEvent(event);
public boolean onInterceptTouchEvent(MotionEvent event) {
return swipeable && super.onInterceptTouchEvent(event);
public boolean isSwipeable() {
return swipeable;
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
* Disable key event prevent user use direction key changing page.
// @Override
// public boolean executeKeyEvent(KeyEvent event) {
// return false;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment