Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Created April 22, 2019 08:33
Show Gist options
  • Save tizisdeepan/9a9423d54240f9ea68e80f812f4bca29 to your computer and use it in GitHub Desktop.
Save tizisdeepan/9a9423d54240f9ea68e80f812f4bca29 to your computer and use it in GitHub Desktop.
A ViewPager component that has no animation while switching between pages.
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
class AnimationlessViewpager : androidx.viewpager.widget.ViewPager {
var swipeLocked: Boolean = true
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
override fun onTouchEvent(event: MotionEvent): Boolean = !swipeLocked && super.onTouchEvent(event)
override fun onInterceptTouchEvent(event: MotionEvent): Boolean = !swipeLocked && super.onInterceptTouchEvent(event)
override fun canScrollHorizontally(direction: Int): Boolean = !swipeLocked && super.canScrollHorizontally(direction)
override fun setCurrentItem(item: Int) {
super.setCurrentItem(item, false)
}
override fun setCurrentItem(item: Int, smoothScroll: Boolean) {
super.setCurrentItem(item, false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment