Skip to content

Instantly share code, notes, and snippets.

@drakeet
Last active February 22, 2018 01:42
Show Gist options
  • Save drakeet/ec4287b17ea4c36c974afdf183e8a7c0 to your computer and use it in GitHub Desktop.
Save drakeet/ec4287b17ea4c36c974afdf183e8a7c0 to your computer and use it in GitHub Desktop.
import android.support.v7.widget.RecyclerView
/**
* @author drakeet
*/
class QuickReturnDelegate(private val scrollSlop: Int = 12) : RecyclerView.OnScrollListener() {
private lateinit var callback: (hide: Boolean) -> Unit
private var scrollY: Int = 0
fun attach(recyclerView: RecyclerView, callback: (hide: Boolean) -> Unit) {
this.callback = callback
recyclerView.addOnScrollListener(this)
}
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
scrollY = 0
}
}
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
scrollY += dy
callback(scrollY > scrollSlop)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment