Created
May 3, 2021 20:33
-
-
Save vitkidd/d3acf4a8c5546e2b90492ff6dcf387be to your computer and use it in GitHub Desktop.
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.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
/** | |
* ViewPager that blocks swiping if contains other swiping view. | |
*/ | |
public class HackyViewPager extends ViewPager { | |
private boolean isLocked; | |
public HackyViewPager(Context context) { | |
super(context); | |
isLocked = false; | |
} | |
public HackyViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
isLocked = false; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
if (!isLocked) { | |
try { | |
return super.onInterceptTouchEvent(ev); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
return false; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
return !isLocked && super.onTouchEvent(event); | |
} | |
public void toggleLock() { | |
isLocked = !isLocked; | |
} | |
public void setLocked(boolean isLocked) { | |
this.isLocked = isLocked; | |
} | |
public boolean isLocked() { | |
return isLocked; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment