2017-01-04 197 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());