android - RecyclerView 项目在滚动时显示相同的背景颜色
问题描述
我是 android 新手,我的回收站视图有问题。我希望使用随机颜色作为个人资料图像背景,但是当我上下滑动几次时颜色会不断变化,我希望每个项目都保持特定的颜色。请问我怎样才能做到这一点。这是我为随机化颜色所做的:
private fun changeClientProfileBackgroundColor ():Drawable?{val colorDrawables = arrayOf(
R.drawable.client_list_background_color_drawable_1,
R.drawable.client_list_background_color_drawable_2,
R.drawable.client_list_background_color_drawable_3,
R.drawable.client_list_background_color_drawable_4,
R.drawable.client_list_background_color_drawable_5,
R.drawable.client_list_background_color_drawable_6 )return ContextCompat.getDrawable(
context!!, colorDrawables[(Math.random() * 5).roundToInt()])}
binding.clientHomeImageProfileColorBackground.background =
changeClientProfileBackgroundColor()
解决方案
如果您的适配器视图不稳定,请尝试在 recyclerview 适配器中使用getItemViewType 。
检查 stackoverflow 中的这个先前的问题以获取更多参考。
推荐阅读
- javascript - 在视频的右下角添加内部 div
- regex - 正则表达式:在两个单引号之间获取第二个字符串
- javascript - 将类添加到加载到页面中的元素
- javascript - 以角度从数组中检索值
- hibernate - 有什么方法可以从存储库中的 Spring Security 获取当前 userId?
- html - Bootstrap 4 固定和粘性标题
- python - plt.gca() 与我的图表一起打开一个空白画布
- python - 如何使用计时器更新 axvline?
- awk - 从每个文件中提取相同的列到一个文件
- vue.js - SVG 在 Safari 上的绘制方式不同