2014-11-06 156 views
0

我似乎無法弄清楚如何使一個或多個listview項目背景更改顏色。Android ListView設置項背景

我目前正試圖通過適配器來獲取視圖。

ViewGroup v = (ViewGroup) adapter.getView(i, null, listView); 
for(int k = 0; k < v.getChildCount(); k++) { 
    View child = v.getChildAt(k); 
    child.setBackgroundColor(0xFFA6D2FF); 
} 
listView.invalidateViews(); 

我已經嘗試設置包含文本項目的ViewGroup V(列表視圖中的項目有子項目所以它的2個文本視圖)。我也嘗試設置可能正在工作的兒童背景,但看起來它們的界限是0。所以它可能正在工作,但即使您可以看到文字,孩子們也沒有大小。

+0

給一個id行項目的根視圖,並嘗試通過設置背景。 – Chitrang 2014-11-06 04:40:06

+0

嘗試在xml中設置背景列表項。 – 2014-11-06 04:40:14

+0

XML不是一個選項。 Chitrang,你是什麼意思的行項目的根視圖?你的意思是從getView返回的視圖的根視圖? – lostdev 2014-11-06 04:46:37

回答

1

更好地從定製的基礎轉接器更改項目的顏色創建以下方法:

private int[] colors; 

// In Constructor or whenever you get data 
colors = new int[sizeOfList]; 

private void setColors(int[] positions, int color) { 
    for (int pos : positions) { 
    colors[pos] = color; 
    } 
    notifyDataSetChanged(); 
} 

private void setColors(int position, int color) { 
    colors[pos] = color; 
    notifyDataSetChanged(); 
} 

public View getView(...) { 

    if (colors[position] != 0) { 

    child.setBackgroundColor(colors[position]); 

    } else { 

    child.setBackgroundColor(// default color); 

    } 

    return view; 

} 

希望它能幫助。我認爲其他答案也建議你做同樣的事情。 :)

+0

像魅力一樣工作。謝謝! – lostdev 2014-11-06 05:29:40

0

如果我正確地理解了您的代碼,您希望爲背景顏色提供每個您在列表項中的視圖。

如果你想這樣做編程(不通過XML),我能想到的最簡單的方法是創建一個自定義的適配器並設置holder(視圖)背景顏色有。

當然,您可以通過XML輕鬆完成此操作。

有關更多信息,請參見本:http://www.vogella.com/tutorials/AndroidListView/article.html#tutorial_ownadapter

隨意評論,如果你有一些問題,或者如果我想念你明白。

0

當您調用listView.invalidateViews();時,它將使用適配器的getView()方法並繪製視圖。如果你想在ListView中改變視圖的背景顏色,最好的地方應該是在你的適配器的getView()中完成。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false); 
    rowView.setBackgroundColor(0xFFA6D2FF); 
    return rowView; 
    } 
+0

我該如何告訴列表適配器哪些應該在那裏更新?這些項目可以通過用戶交互進行更新。 – lostdev 2014-11-06 04:45:19

+0

如果convertView爲null,您應該理想地誇大新視圖。 – 2014-11-06 04:45:57

+0

您的模型中應該有一個與ListView綁定的稱爲背景顏色的字段。你可以改變模型,然後調用'notifyDatasetChanged()'強制ListView重繪。 – 2014-11-06 04:48:16

0

據我所知。這應該通過在自定義適配器的getView方法中創建自定義視圖來完成。

公共類MyAdapter延伸BaseAdapter {

 public Activity activity; 
    ArrayList<String> data = new ArrayList<String>(); 
    private static LayoutInflater inflater = null; 

    public MyAdapter(Activity a, ArrayList<String> d) { 
     activity = a; 
     data = d; 
     inflater = LayoutInflater.from(activity); 
     }  

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

     ViewHolder holder = null; 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.my_list_item, null); 
      holder = new ViewHolder(); 
      convertView.setTag(holder); 

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

    convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      //Touch on view handle 
     Log.d("", "Touched row "+position); 
     } 

    }); 

    //customizing view 
    holder.textView = (TextView)convertView.findViewById(R.id.my_textview); 
    holder.textView.setText(data.get(position)); 

    return convertView; 
}  

    public static class ViewHolder { 
    public TextView textView; 
    } 
@Override 
    public int getCount() { 
    return data.size(); 
    } 
@Override 
public Object getItem(int position) { 
    return position; 
} 
@Override 
public long getItemId(int position) { 
    return position; 
} 

}

Background each row in listview