简体   繁体   English

ListView android松散背景色

[英]ListView android looses background color

I have an adapter which extends ArrayAdapter> filling fragment layout with multiple choices each time. 我有一个适配器,它可以扩展ArrayAdapter>填充片段布局,每次都有多个选择。 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 如果您查看此图片http://i.imgur.com/S1KEfM1.png

you can see that ccc option lost background. 您会看到ccc选项丢失了背景。 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 具有此背景的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 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 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. 而且new_strip基本上只是带有边框的白色背景图像,而list_focused_example是选中复选框的图像。

GetView in my class 我班的GetView

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). 最后,问题是图像(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. 我必须使一个可绘制的图像与该图像相同(它只有白色背景和灰色圆角),以便解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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