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.

相关推荐
summerkissyou198738 分钟前
Android - 摄像头 - hal - 开发教程,例子,常见问题,分析方法,解决方案
android
summerkissyou19871 小时前
Android 16 架构图
android
神龙天舞20012 小时前
MySQL 备库为什么会延迟好几个小时
android·数据库·mysql
码农coding5 小时前
android12 systemUI 之锁屏
android
圆山猫5 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v
爱笑鱼6 小时前
Binder(七):getCallingUid() 读到的是谁?clearCallingIdentity() 又清掉了什么?
android
2501_9159090611 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
Lvan的前端笔记12 小时前
AndroidX 完全入门指南
android·androidx
帅次12 小时前
Android 应用高级面试:Window 近1年高频追问 18 题
android·面试·职场和发展
总捣什么乱113 小时前
MySQL MySQL是怎么保证主备一致的?
android·mysql·adb