效果图
1.关键代码
1.
// 定义一个变量来记录滑动的距离var scrollDistance = 0// 在RecycleView的滑动监听器中更新滑动的距离binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {super.onScrolled(recyclerView, dx, dy)// 累加滑动的距离scrollDistance += dy// 根据滑动的距离设置背景颜色if (scrollDistance > 100) {binding.button.setBackgroundColor(Color.GRAY)recyclerView.setBackgroundColor(Color.GRAY)} else {binding.button.setBackgroundColor(Color.WHITE)recyclerView.setBackgroundColor(Color.WHITE)}}})