颜色数组中列表视图中每一行的随机颜色

Random Color for each row in a list view from array of colors

我将自定义适配器中每一行的背景颜色设置为

public View getView(int position, View convertView, ViewGroup parent) {





    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.coupon_list_row, null);
        int temp_index = ExtraFu.randInt(0, 9);
        convertView.setBackgroundColor(color_arr[temp_index]);
        Log.d("temp_index", String.valueOf(temp_index));
    }

我的颜色数组

int color_arr[]={R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};

这是函数 randInt

  public static int randInt(int min, int max) {

    // Usually this can be a field rather than a method variable
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}  

行背景设置为两种颜色。是不是每次生成的随机数都一样?

  if (position%4 == 0){
             // set convertView Background          
        } else if (position%4 == 1){
            // set convertView Background  
        } else if (position%4 == 2){
            // set convertView Background  
        } else if (position%4 == 3){
            // set convertView Background  
        }

这将在您的列表视图中随机生成四种不同的颜色。

内部适配器

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = lv.inflate(res, null);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.text);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position%4 == 0){
            holder.textView.setBackgroundColor(Color.parseColor("#1e86cf"));
        } else if (position%4 == 1){
            holder.textView.setBackgroundColor(Color.parseColor("#2ca0ea"));
        } else if (position%4 == 2){
            holder.textView.setBackgroundColor(Color.parseColor("#2cc4ea"));
        } else if (position%4 == 3){
            holder.textView.setBackgroundColor(Color.parseColor("#2ceae3"));
        }
        return convertView;
    }

ViewHolder Class:

    class ViewHolder{
        public TextView textView;
    }

适配器自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="15dp"
    android:textColor="@color/white"
    android:textSize="20sp"
    android:textStyle="bold"/>
</LinearLayout>

使用下面的代码获取随机颜色

Random rnd = new Random();
        int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        convertView.setBackgroundColor(color);

我已经编辑了你的代码

你需要使用下面的行我已经检查了它的工作finr为我的布局

 convertView.setBackgroundResource(color_arr[rnd]);

我这边没有检查你更新的代码

if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.coupon_list_row, null);



 int color_arr[]=  {R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};
int rnd = new Random().nextInt(color_arr.length);


        //convertView.setBackgroundColor(color_arr[temp_index]);

        convertView.setBackgroundResource(color_arr[rnd]);

    }

参考代码是我已经尝试过并且工作正常所以做必要的更改

int color_arr[] = {R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark};


    int rnd = new Random().nextInt(color_arr.length);

    linearLayout.setBackgroundResource(color_arr[rnd]); 

对于随机颜色生成:

private int getRandomColor() {
  SecureRandom rgen = new SecureRandom();
  return Color.HSVToColor(150, new float[]{
      rgen.nextInt(359), 1, 1
  });
}

并在检索项目位置后设置每个项目的BackGroundColor

holder.textView.setBackGroundColor(getRandomColor());

小贴士:使用模运算符,给每一行涂上4种颜色

export const moduloColor = (rowIndex: number): string => {
let rep
    if (rowIndex % 4 === 0) { // if rowIndex is a multiple of 4
        rep = '#00838F'
    } else if (rowIndex % 3 === 0) { // if rowIndex is a multiple of 3
        rep = '#00ACC1'
    } else if (rowIndex % 2 === 0) { // if rowIndex is a multiple of 2
        rep = '#26C6DA'
    } else {
        rep = '#80DEEA'
    }
return rep
}