Android - custom listView, each row different colours

安稳与你 提交于 2020-01-15 10:56:26

问题


I have created custom listview, each row looks like my file custom_row.xml. Is there any way, how to set up different background colors for each row separately (I need to set it because of different values my rows can have).

Thanks for any idea


回答1:


Since your doing custom listview in the getView method after inflating your custom_row.xml change the background of the return view of inflate method. See sample snippet below:

public getView(int position, View convertView, ViewGroup parent) {
       convertView = getLayoutInflater().inflate(R.layout.custom_xml, null);
       do some stuff...

       //let say you have an arraylist of color
       convertView.setBackgroundColor(arraylist.get(position));

       //in case that your color is limited, just re-use your color again
       //and some logic how to re-use the colors.
}



回答2:


In the adapter, you can manually set the background color of the view when the view is retrieved using the getView method.

    // set the background to green
    v.setBackgroundColor(Color.GREEN);



回答3:


Using the other answers here prevented the selector to work properly and the selector stopped highlighting the rows completely. By setting the colors manually as described in the accepted answer, the selector stopped highlighting rows while scrolling. So let me describe a solution that does not mess your selector.

This article is describing how to fix this with transparency, but I could not really make it work. So the solution for me was that I have two list selectors in my drawable folder. This way I can set the two different background colors at runtime and keep the selector working.

list_selector_darkgrey.xml for my dark grey line

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/gradient_bg_darkgrey" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="true"/>
<item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="false" android:state_selected="true"/>
</selector>

and list_selector.xml for the light grey line

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/gradient_bg" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="true"/>
<item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="false" android:state_selected="true"/>
</selector>

Please ignore the gradients in drawable, you can replace it with your colors.

In the BaseAdapter class, I will call the setBackgroundResource to display some rows as light grey and other rows as dark grey. The selector color is the same and defined in the XML file.

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

             ... some logic ...

    if (... some condition ...) {
        vi.setBackgroundResource(R.drawable.list_selector_darkgrey);
    }
    else { 
        vi.setBackgroundResource(R.drawable.list_selector);
    }
    return vi;
}


来源:https://stackoverflow.com/questions/10213789/android-custom-listview-each-row-different-colours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!