recycleView(三)动态修改背景色

效果图

1.关键代码

1.

kotlin 复制代码
        // 定义一个变量来记录滑动的距离
        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)
                }
            }
        })

3.总结

1.

相关推荐
普通人65 小时前
MySQL_FIND_IN_SET函数详解
android·数据库·mysql
好的佩奇17 小时前
Dart 之集合元素
android·flutter·dart
Easyhood18 小时前
Android 开发入门实践(十四):Compose 副作用入门与四个常用 API
android·开发语言·kotlin·android jetpack·compose
FlightYe19 小时前
音视频修炼之容器与协议(四):RTSP详解
android·linux·ffmpeg·音视频·dash
疯华整猫20 小时前
删掉 LeakCanary 那天,我用 Trae 自己撸了一套:最难的不是分析堆,是决定什么时候 dump
android·人工智能·性能优化
mmsx1 天前
耳切法切带洞多边形:论文能跑通,真实数据为什么崩
android·kotlin
洞见前行1 天前
你的 Activity 关了,为什么还活着?Android 内存泄漏从发现、定位到线上治理
android·性能优化·leakcanary
Kapaseker1 天前
Kotlin 又要加语法糖了:这次轮到 if-else
android·kotlin
恋猫de小郭1 天前
Flutter 3.50 & Dart 3.14 发布,快来看看有什么更新吧
android·前端·flutter