Skip to content

Instantly share code, notes, and snippets.

@EdwardvanRaak
Last active October 7, 2019 12:33
Show Gist options
  • Save EdwardvanRaak/7d431f83801c72355d31598f4d8f1c66 to your computer and use it in GitHub Desktop.
Save EdwardvanRaak/7d431f83801c72355d31598f4d8f1c66 to your computer and use it in GitHub Desktop.
//Custom refresh layout without memory leak
class ViewPagerSwipeRefreshLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : SwipeRefreshLayout(context, attrs) {
private val touchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
private var previousX = 0F
private var declined: Boolean = false
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
previousX = event.x
declined = false
}
MotionEvent.ACTION_MOVE -> {
val eventX = event.x
val xDiff = abs(eventX - previousX)
if (declined || xDiff > touchSlop) {
declined = true
return false
}
}
}
return super.onInterceptTouchEvent(event)
}
}
//Recyclerview
swipeRefreshLayout.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
swipeRefreshLayout?.isEnabled = isSwipeRefreshEnabled(recyclerView)
}
})
private fun isSwipeRefreshEnabled(recyclerView: RecyclerView?): Boolean {
val topRowVerticalPosition =
if (recyclerView == null || recyclerView.childCount == 0) 0
else recyclerView.getChildAt(0).top
return topRowVerticalPosition >= 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment