Created
March 3, 2016 16:15
-
-
Save hector6872/c16009b04dc84130a1e1 to your computer and use it in GitHub Desktop.
LockableViewPager
This file contains hidden or 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
<declare-styleable name="LockableViewPager"> | |
<attr name="locked" format="boolean"/> | |
</declare-styleable> |
This file contains hidden or 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
public class LockableViewPager extends ViewPager { | |
private static final boolean DEFAULT_LOCK_STATE = false; | |
private boolean locked; | |
public LockableViewPager(Context context) { | |
this(context, null); | |
} | |
public LockableViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
final TypedArray typedArray = | |
context.obtainStyledAttributes(attrs, R.styleable.LockableViewPager); | |
locked = typedArray.getBoolean(R.styleable.LockableViewPager_locked, DEFAULT_LOCK_STATE); | |
typedArray.recycle(); | |
} | |
@Override public boolean onTouchEvent(MotionEvent event) { | |
return locked || super.onTouchEvent(event); | |
} | |
@Override public boolean onInterceptTouchEvent(MotionEvent event) { | |
return locked || super.onInterceptTouchEvent(event); | |
} | |
public boolean isLocked() { | |
return locked; | |
} | |
public void setLocked(boolean locked) { | |
this.locked = locked; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if locked is true,the view inside viewpager will nerver get event any more...
You should try: !locked && super.onInterceptTouchEvent(event)