A Custom ViewPager which can been disable swipe.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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