2015-02-09 120 views
2

我有一個適配器,它擴展了ArrayAdapter>填充片段佈局,每次都有多個選擇。 問題是,有時背景會在項目選擇之前完全消失,或者所有其他項目都會消失。如果你檢查這個圖像 http://i.imgur.com/S1KEfM1.pngListView android丟失背景顏色

你可以看到ccc選項丟失背景。佈局膨脹這個相對佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="67dp" 
android:background="@drawable/selectable_background_example" 
android:descendantFocusability="blocksDescendants" 
android:gravity="center_vertical" 
android:paddingLeft="20dp" 
android:paddingRight="10dp" > 

在這種背景下XML

<item android:drawable="@drawable/list_focused_example" android:state_focused="true" android:state_pressed="false"/> 
<item android:drawable="@drawable/pressed_background_example" android:state_pressed="true"/> 
<item android:drawable="@drawable/pressed_background_example" android:state_selected="true"/> 
<item android:drawable="@drawable/pressed_background_example" android:state_activated="true"/> 
<item android:drawable="@drawable/new_strip"/> 

Pressed_background_example

<solid android:color="@color/pressed_example" /> 

<stroke 
    android:width="3dp" 
    android:color="@color/pressed_example" /> 

<padding 
    android:bottom="1dp" 
    android:left="1dp" 
    android:right="1dp" 
    android:top="1dp" /> 

Pressed_example

<color name="pressed_example">#CCCC0000</color> 

而且new_strip基本上是與邊境地區的背景白色圖像,並list_focused_example是複選框擠壓的圖像。

GetView在我的課

公共查看getView(INT位置,查看convertView,ViewGroup以及母公司){ 持有人mhHolder;

 if (convertView == null) { 
      mhHolder = new Holder(); 
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_a, parent, false); 
      mhHolder.txtValue = (TextView) convertView.findViewById(R.id.txtValue); 
      mhHolder.txtCheckbox = (CheckBox) convertView.findViewById(R.id.checkBox1); 
      mhHolder.txtText = (TextView) convertView.findViewById(R.id.txtText); 
      convertView.setTag(mhHolder); 
     } else { 
      mhHolder = (Holder) convertView.getTag(); 
     } 
     HashMap<String, String> hm = values.get(position); 

     mhHolder.txtValue.setText(hm.get("Value")); 
     mhHolder.txtText.setText(hm.get("Text")); 
     mhHolder.txtCheckbox.setVisibility(View.VISIBLE); 
     //TODO CHANGE WHEN CHECKED IS SEND  
     mhHolder.txtCheckbox.setChecked(Base.checked[position]); 

有沒有人有任何線索,爲什麼以前選定項目或有時除了選擇一個所有項目背景自敗。

+0

你能發表list_focused_example和new_strip代碼嗎? – 2015-02-09 18:30:02

+0

嗨!如上所述,這只是圖像。 new_strip是您可以在每個字段周圍看到的背景圖像(具有灰色圓形邊框的白色背景,這基本上會消失)。而list_focused_xample只是一個按下的複選框圖片 – Tomek 2015-02-10 09:41:16

回答

0

最後問題是圖像(new_strip)。我必須繪製與圖像(只有白色背景和灰色圓角)相同的drawable,以便解決問題。

0

我認爲這是一種錯誤(或者我真的沒有真正的功能)。

總之你應該嘗試,以便它是根據每個項目執行調用

mhHolder.txtValue.setbackground(R.drawable.selectable_background_example); 

在你的代碼。

+0

Nope沒有修復它。添加到txtValue只是將背景添加到EditText子項,然後我有2個背景,問題仍然出現。將backgroundResource添加到convertView並沒有做更好的事情,仍然存在問題。我忘了提及,如果在手機上測試過,但在平板電腦上則不行 – Tomek 2015-02-09 17:16:23