我需要你的帮助。我要做的是重复颜色数组(蓝色、红色、绿色),直到它与适配器的计数相匹配。
这是我的样本:
public static final int[] colors = { R.color.main_blue, R.color.main_red, R.color.green_button };
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.verticalLine.setBackgroundColor(colors[position]);
}
但它会超出数组,因为它只有 3 个元素。 (ArrayOutOfBoundsException) 对实现这个有什么帮助吗?我很乐意感谢你的帮助。谢谢!
最佳答案
holder.verticalLine.setBackgroundColor(colors[position%3]);
使用模 3(如果将位置除以 3,则得到其余部分。例如 1%3 = 1, 5%3 = 2, 6%3 = 0
关于Android 重复数组直到匹配适配器的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33751507/