【问题标题】:Change background color of each row in ExpandableListView?更改 ExpandableListView 中每一行的背景颜色?
【发布时间】:2011-08-06 11:27:32
【问题描述】:

嘿,是否可以更改 ExpandableListView 中指定行的背景?我想要的是:第一排蓝色,第二排红色,第三排蓝色,第四排红色.....谢谢

【问题讨论】:

    标签: java android


    【解决方案1】:

    是的,这是可能的,您必须为展开式列表视图使用自定义适配器。

     @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;
        }
    

    阅读this 文章,此处还提供示例和源代码。

    【讨论】:

    • 我收到此错误:“Main2 类型的方法 getChildView(int, int, boolean, View, ViewGroup) 必须重写或实现超类型方法”.. 这可能是因为我正在扩展我的类到 Activity 而不是 BaseExpandableListAdapter ?
    【解决方案2】:

    您可能想查看this thread: Android ExpandableListView
    有一个 working sample 具有您想要的功能(根据它们保存的数据为行着色)。

    【讨论】:

      【解决方案3】:

      当然。您只需在创建或重新创建视图时手动设置视图的背景。在方法中的适配器中执行此操作

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

      基于组和子位置

      【讨论】:

        猜你喜欢
        • 2020-10-01
        • 1970-01-01
        • 2013-11-16
        • 1970-01-01
        • 2013-09-03
        • 2011-11-02
        • 2014-04-13
        • 2011-04-21
        • 2012-02-25
        相关资源
        最近更新 更多