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.

相关推荐
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
雨白1 小时前
Android 建造者模式实战:从 AlertDialog 源码到 TopBar 的两种实现
android
_祝你今天愉快4 小时前
Android 高级绘制:[五] 贝塞尔曲线与计算规则
android
FungLeo19 小时前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks19 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui1 天前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白2 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android2 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m2 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui2 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android