简体   繁体   中英

ListView android looses background color

I have an adapter which extends ArrayAdapter> filling fragment layout with multiple choices each time. The problem is that sometimes background disappears completely on items choose before, or all other items. If you check this image http://i.imgur.com/S1KEfM1.png

you can see that ccc option lost background. Layout inflates this Relative Layout

<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" >

With this background xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <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"/>
</selector>

Pressed_background_example

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <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" />

</shape>

Pressed_example

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

And new_strip is basically just that background white image with border, and list_focused_example is image for checkbox pressed.

GetView in my class

public View getView(int position, View convertView, ViewGroup parent) {
    Holder 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]);
}

Does anyone have any clue on why the background dissapears on previous selected items or sometimes on all items except the selected one.

I think it´sa kind of bug (or I don't really get the real function).

Anyhow you should try to call

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

in your code so that it is executed for each item.

In the end the problem was the image (new_strip). I had to make a drawable the same as the image (which had only white background and gray rounded corners), so that fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM