以特定的重复模式设置列表视图项目的颜色

Aas*_*hir 0 android listview loops android-listview

我想知道如何创建一个循环,以重复出现的模式为每个列表视图项目设置特定的背景颜色。假设我有 10 件物品和 4 种颜色,我希望这 10 件物品按照图案的顺序着色并重复,直到所有物品都着色。考虑到我在整数数组中有以下颜色:

int[] colours = {Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA};
Run Code Online (Sandbox Code Playgroud)

ant*_*iom 5

好吧,我会为此实现一个自定义Adapter或一个。ViewBinder

Adapter例如,如果是 an ,ArrayAdapter您必须重写该getView方法

...
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = getActivity().getLayoutInflater().inflate(R.layout.item, null);
    }

    view.setBackgroundColor(colors[position % colors.length]);

    return view;
}
....
Run Code Online (Sandbox Code Playgroud)