简体   繁体   中英

Change background color of each row in ExpandableListView?

Hey, Is it possible to change the background of a specified row in an ExpandableListView? What I want is: 1st row blue, 2nd red, 3rd blue, 4th red..... Thanks

Yes this is possible, and you have to use custom adapter for your Expandable list view.

 @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
       //......
        //...... Your code for row view
       //.....

        //Now write here code for set colors for rows
        if(childPosition % 2 == 0) {
           convertView.setBackgroundColor("#0000FF"); // use this when you want to use hexa value of colors
        } else {
           convertView.setBackgroundColor("#FF0000"); // use this when you want to use hexa value of colors
        }

        //setBackgroundResource(android.R.color.transparent); // use this for predefined colors at place of setBackground


       return convertView;
    }

Read this article, here example and source code also available.

Of course. You will simply need to manually set the background on the views as you create or recreate them. Do this in the adapter in the method

getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

Based on the group and child position

You might want to check out this thread: Android ExpandableListView .
There is a working sample with the functionality you want (colorize the rows based on the data they hold).

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