Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Created April 22, 2019 08:26
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 tizisdeepan/c8af205cc9a97508157da10e99e5de77 to your computer and use it in GitHub Desktop.
Save tizisdeepan/c8af205cc9a97508157da10e99e5de77 to your computer and use it in GitHub Desktop.
A ViewPager component that disables swipe navigation.
import android.content.Context
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import android.util.AttributeSet
class LockableViewPager : ViewPager {
var swipeLocked: Boolean = true
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
override fun onTouchEvent(event: MotionEvent): Boolean {
return !swipeLocked && super.onTouchEvent(event)
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
return !swipeLocked && super.onInterceptTouchEvent(event)
}
override fun canScrollHorizontally(direction: Int): Boolean {
return !swipeLocked && super.canScrollHorizontally(direction)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment