开发者

Change background color of each row in ExpandableListView?

开发者 https://www.devze.com 2023-02-26 02:57 出处:网络
Hey, Is it possible to change the background of a specified ro开发者_Go百科w in an ExpandableListView? What I want is: 1st row blue, 2nd red, 3rd blue, 4th red.....ThanksYes this is possible, and you

Hey, Is it possible to change the background of a specified ro开发者_Go百科w 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.


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消