Last active
February 22, 2018 01:42
-
-
Save drakeet/ec4287b17ea4c36c974afdf183e8a7c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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