MaterialScrollBar
MaterialScrollBar copied to clipboard
Is there a way to hide the handle when the list is not being scrolled?
When list is not in motion hide the handle. When list comes back to motion show the handle but in a fading manner.
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
val timer = Timer()
var task: TimerTask? = null
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
when (newState) {
RecyclerView.SCROLL_STATE_DRAGGING -> {
task?.cancel()
timer.purge()
task = null
if (binding.scrollBar.alpha != 1f) {
ObjectAnimator.ofFloat(binding.scrollBar.alpha, 1f).apply {
addUpdateListener { binding.scrollBar.alpha = it.animatedValue as Float }
duration = 250L * (1L - binding.scrollBar.alpha.toLong())
start()
}
}
}
RecyclerView.SCROLL_STATE_IDLE -> {
task = timerTask {
if (binding.scrollBar.isDragging) return@timerTask
activity?.runOnUiThread {
ObjectAnimator.ofFloat(1f, 0f).apply {
addUpdateListener { binding.scrollBar.alpha = it.animatedValue as Float }
duration = 250
start()
}
}
}
timer.schedule(task, 500)
}
}
}
})