Skip to content

Instantly share code, notes, and snippets.

Embed
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.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