Skip to content

Instantly share code, notes, and snippets.

@mreram
Last active Aug 19, 2018
Embed
What would you like to do?
package eram.ir.camerapager
import android.animation.ValueAnimator
import android.app.Activity
import android.content.Context
import android.support.v4.app.FragmentActivity
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.DecelerateInterpolator
import android.widget.Scroller
class CViewPager @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : ViewPager(context, attrs) {
var enabledScroll = true
lateinit var exitListener: ExitListener
init {
setMyScroller()
}
fun setOnExitListener(exitListener: ExitListener) {
this.exitListener = exitListener
}
override fun onTouchEvent(ev: MotionEvent?): Boolean {
return super.onTouchEvent(ev) && (onDrag(ev))
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return if (enabledScroll) super.onInterceptTouchEvent(ev) else false
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
return super.dispatchTouchEvent(ev) && (onDrag(ev))
}
var startPointY = 0f
var prevY = 0f
var startPointX = 0f
private fun onDrag(ev: MotionEvent?): Boolean {
when (ev!!.action) {
MotionEvent.ACTION_DOWN -> {
prevY = getItemViewAt(currentItem).y
startPointY = ev.rawY
startPointX = ev.rawX
}
MotionEvent.ACTION_MOVE -> {
Log.i("current child", (context as FragmentActivity).supportFragmentManager.findFragmentByTag("android:switcher:" + id + ":"
+ currentItem)::class.java.canonicalName)
if (ev.action == MotionEvent.ACTION_MOVE && !enabledScroll && ev.rawY - startPointY > 0) {
getItemViewAt(currentItem).y = ev.rawY - startPointY
return enabledScroll
}
}
MotionEvent.ACTION_UP -> {
if (::exitListener.isInitialized && !enabledScroll)
if (getItemViewAt(currentItem).y > ev.y / 3) {
animateToRelease()
exitListener.onExit()
} else {
animateToRelease()
}
return (ev.x - startPointX) == 0f
}
}
return true
}
private fun getItemViewAt(index: Int): View {
val fragment = (context as FragmentActivity).supportFragmentManager.findFragmentByTag("android:switcher:" + id + ":"
+ currentItem)
return fragment.view!!
}
private fun animateToRelease() {
val animator = ValueAnimator.ofFloat(getItemViewAt(currentItem).y, prevY)
animator.addUpdateListener { animation ->
getItemViewAt(currentItem).y = animation.animatedValue as Float
}
animator.interpolator = DecelerateInterpolator()
animator.duration = 500
animator.start()
}
private fun setMyScroller() {
try {
val viewpager = ViewPager::class.java
val scroller = viewpager.getDeclaredField("mScroller")
scroller.isAccessible = true
scroller.set(this, MyScroller(context))
} catch (e: Exception) {
e.printStackTrace()
}
}
inner class MyScroller(context: Context) : Scroller(context, DecelerateInterpolator()) {
override fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int, duration: Int) {
super.startScroll(startX, startY, dx, dy, if (enabledScroll) 200 else 500 /*1 secs*/)
}
}
interface ExitListener {
fun onExit()
}
}
@mreram
Copy link
Author

mreram commented Aug 19, 2018

Custom View Pager with some gestures and animations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment