我ListView項有一個自定義適配器列表視圖。我選擇後,ListView項目的背景顏色變化了。這在Samsung Galaxy S2上的Ice Cream Sandwich 4.0.4和仿真器上的ICS 4.0.3上完美運行。但它不適用於4.2.2 JellyBean模擬器。在JB上,當一個項目被選中時,它就像之前一樣保持背景。setBackgroundColor不工作的豆形軟糖
這是我的代碼的一部分,我有選擇項時,設置背景顏色的邏輯:
private int mItemIndex = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
....
if (convertView == null) {
....
....
} else {
/* To highlight the selected item */
if (position == mItemIndex) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(context.getResources().getColor(R.color.SkyBlue));
} else {
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(context.getResources().getColor(R.color.WhiteSmoke));
}
/* To highlight the selected item - end */
....
....
return v;
}
另外,我試過 convertView.setBackgroundResource(context.getResources().getColor(R.color.SkyBlue));
,而不是setBackgroundColor並沒有工作過。
有沒有得到這個在上班的Jellybean一個解決方法嗎? 還是我失去了在代碼中的東西嗎?
謝謝。
試試這個http://stackoverflow.com/questions/15253987/inflate-listview-row-from-onclicklistener-in-android/15254297#15254297 – Pragnani 2013-03-23 14:07:33
你是否只需要這種顏色更改來突出顯示ListView中的選定項目? – Droidman 2013-03-23 14:58:04
@Pragnani,謝謝。我看到你告訴我有一個布爾數組,以保持狀態的粘性,但我不完全理解如何實現它在我的情況。從我的活動具有ListView控件我打電話,'listView.setOnItemClickListener(新OnItemClickListener() \t {公共無效onItemClick(適配器視圖>爲arg0,ARG1觀,詮釋ARG2,長ARG3){ \t \t((TransactionListAdapter)listView.getAdapter ()); setSelectItem(arg2); \t \t} \t});''和'setSelectItem()'設置'mItemIndex'和傳入的參數arg2。我的適配器類中應該有布爾數組嗎? – Bharat 2013-03-23 14:59:43