【问题标题】:Random Color for each row in a list view from array of colors来自颜色数组的列表视图中每一行的随机颜色
【发布时间】:2017-05-18 06:36:05
【问题描述】:

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

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条件下的位置编号和颜色数组。 with 在循环中设置背景颜色以转换视图。
  • 我不能这样做,因为列表是动态的,可以包含 n 个项目。

标签: android listview custom-adapter


【解决方案1】:
  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 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>

【讨论】:

  • 那么如果我有 n 个项目?那个随机数有什么问题?
  • 在我的回答中它将重复四种颜色。它可能会随机变化。
  • 我已经做了你写的同样的事情。但我使用 10 而不是 4,但仍然只得到两种不同的颜色
  • 你可能在检查你的代码时犯了任何错误。此代码在我的应用中运行良好。
  • 逐步尝试,最初尝试 4 和 6,然后移动到 10。
【解决方案2】:

我已经编辑了你的代码

您需要使用下面的行,我已经检查了我的布局的工作 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]); 

【讨论】:

    【解决方案3】:

    对于随机颜色生成:

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

    并在检索到item位置后设置每个item的BackGroundColor

    holder.textView.setBackGroundColor(getRandomColor());
    

    【讨论】:

      【解决方案4】:

      使用以下代码获取随机颜色

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

      【讨论】:

      • 这只会生成 2 种颜色。
      • 否,此代码将随机生成所有颜色,而不仅仅是两种颜色。但随机,以便它会随机重复颜色。你可以再检查一次。如果你对颜色没有任何限制,那么你可以使用上面的代码。
      猜你喜欢
      • 2017-09-08
      • 2016-01-18
      • 2013-02-03
      • 2015-04-09
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多