2013-03-23 40 views
1

我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一個解決方法嗎? 還是我失去了在代碼中的東西嗎?

謝謝。

+1

試試這個http://stackoverflow.com/questions/15253987/inflate-listview-row-from-onclicklistener-in-android/15254297#15254297 – Pragnani 2013-03-23 14:07:33

+0

你是否只需要這種顏色更改來突出顯示ListView中的選定項目? – Droidman 2013-03-23 14:58:04

+0

@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

回答

0

採取用戶自定義選擇與選擇和非選擇

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/pause_button" 
      android:state_selected="true" /> 
    <item android:drawable="@drawable/play_button" /> 
</selector> 

1.創建一個全局變量兩種不同的狀態圖像在您的活動

View previous;(把你的listrow的ViewGroup中)

而在你onCreate方法初始化這樣的setContentView後

previous=new View(context); 

而在你的ListView onItemClick聽衆做這樣

list.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> arg0, View view, 
         int arg2, long arg3) { 
        view.setSelected(true); 
             previous.setSelected(false); 
             previous=current; 
       } 
      }); 
+0

目前我沒有自定義選擇器,但爲listview的行自定義視圖:https://gist.github.com/bharatkrishna/5175158 我會看看我可以如何將其轉換爲選擇器。對不起,我是Android開發人員的業餘愛好者。將嘗試它並在這裏發表我的評論。謝謝。 – Bharat 2013-03-23 16:46:31

+0

@RBK如果您對Custom Drawable選擇器沒有想法,那麼直接設置背景顏色而不是setSelected,即在上面的答案視圖中。setBackgroundColor(yourcolor); ,對於之前設置的正常listitem的顏色,當它沒有突出顯示時 – Pragnani 2013-03-23 17:44:10

+0

謝謝!我做了你所說的和它的工作。這是我的setOnItemClickListner:http://pastebin.com/4CPwicb0。 順便說一句,在你的回答中,我認爲你的意思是'previous = view'而不是'previous = current'。 – Bharat 2013-03-23 21:02:44

相關問題