2017-01-04 198 views
1

我设置每一行的背景颜色在我customadapter数组列表视图中的每一行作为随机颜色从颜色

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; 
} 

行背景设置为两种颜色。那个随机数是否始终生成一样?

+0

比较位置n条件下的数字和颜色数组。在循环中设置convertview的背景颜色。 –

+0

我不能这样做,因为列表是动态的,可以包含n个项目。 –

回答

2
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"/> 

+0

所以如果我有n个项目?那个随机数有什么问题? –

+0

在我的回答中它会重复四种颜色。随机它可能会改变。 –

+0

我已经完成了你写的东西。但不是4我使用10,但stil得到只有两种不同的颜色 –

0

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

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

但我已经有一组选择的颜色 –

+0

这只产生2种颜色。 –

+1

没有这个代码会随机产生所有的颜色不仅有两种颜色。但是是随机的,以便随机重复颜色。你可以再次检查。如果你没有任何颜色限制,那么你可以使用上面的代码。 –

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]); 
2

对于颜色随机生成:

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

和检索后设置每个项目的BackGroundColor项目位置:

holder.textView.setBackGroundColor(getRandomColor());