我的RecyclerView中包含一些元素。现在,我试图改变每一秒元素的背景颜色,但我的代码不能正常工作......这就是我的方法onBindViewHolder更改RecyclerView中每个第二个元素的颜色
public void onBindViewHolder(CityViewHolder holder, int position) {
String cityName = cityList.get(position);
holder.cityTextView.setText(cityName);
if (position%2 == 0) {
holder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorLightGrey));
}
else {
holder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorGrey));
}
}
什么是你的代码的结果?目前发生了什么? –
我想你有适配器中的活动参考请尝试这个activity.getResources()。getColor(R.color.colorLightGrey)。代码中的所有其他代码看起来都很好 –
输出是什么?添加截图。 – Androider