1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) onRecyclerScrolled(recyclerView, dx, dy) }
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { super.onScrollStateChanged(recyclerView, newState) } })
private val dp180 = dp2px(180) private var distanceY = 0 private var current = 0 private fun onRecyclerScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { distanceY += dy when { distanceY >= dp180 -> { if (current == 1) return recyclerView.setBackgroundColor(Color.argb(255, 246, 248, 250)) current = 1 } distanceY <= 0 -> { if (current == 0) return recyclerView.setBackgroundColor(Color.argb(0, 246, 248, 250)) current = 0 } else -> { recyclerView.setBackgroundColor( Color.argb( distanceY * 255 / dp180, 246, 248, 250 ) ) current = -1 } } }
|