Skip to content

Instantly share code, notes, and snippets.

@Folyd
Last active September 9, 2015 11:52
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 Folyd/ad48e0e4230af336eac0 to your computer and use it in GitHub Desktop.
Save Folyd/ad48e0e4230af336eac0 to your computer and use it in GitHub Desktop.
A Custom ViewPager which can been disable swipe.
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.folyd.app.R;
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"/>
</declare-styleable>
*/
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NonSwipeableViewPager);
swipeable = ta.getBoolean(R.styleable.NonSwipeableViewPager_swipeable, true);
ta.recycle();
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
return swipeable && super.onTouchEvent(event);
}
@Override
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