turing-tech / MaterialScrollBar

An Android library that brings the Material Design 5.1 sidebar to pre-5.1 devices.
Apache License 2.0
781 stars 126 forks source link

Is there a way to hide the handle when the list is not being scrolled? #130

Open nicoqueijo opened 4 years ago

nicoqueijo commented 4 years ago

When list is not in motion hide the handle. When list comes back to motion show the handle but in a fading manner.

turing-tech commented 3 years ago
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)
                    }
                }
            }
        })