---
title: "ExpandableListView修改group图标  "
author: "Java-Object"
date: "2014-08-06T17:33:52Z"
tags: []
categories: ["编程语言"]
slug: "expandablelistview-group"
draft: false
lastmod: "2014-08-06T17:33:52Z"
id: 2101042
url: "https://hezd.iteye.com/blog/2101042"
---

修改group前面的图标有两种方式：

一、通过修改xml文件中groupIndicator更换为@drawable/\**

设置selector

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;selector xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"&gt;  
 &lt;!-- 下拉状态 --&gt;  
    &lt;item android:state\_expanded="false"  
          android:drawingCacheQuality="auto"  
        android:drawable="@drawable/xiala"/&gt;  
    &lt;!-- 收起状态 --&gt;  
    &lt;item android:state\_expanded="true"  
        android:drawingCacheQuality="auto"  
     android:drawable="@drawable/shouqi"/&gt;  
&lt;/selector&gt;

![ExpandableListView修改group图标 - 无尘 - 无尘的博客](http://img.ph.126.net/fNauYLZwASOSgV570cXMSw==/1028228089940931286.png)

但是，这种方式做的话，图标不能根据内容调整大小。

二、通过禁用系统自带图标，在group中添加一个ImageView来实现，并通过判断是否有子项来控制是否显示此图标

1、main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
    android:orientation="vertical"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="fill\_parent"  
    &gt;  
    &lt;!-- 禁用系统自带图标android:groupIndicator="@null" --&gt;  
&lt;ExpandableListView  
    android:layout\_width="fill\_parent"   
    android:layout\_height="wrap\_content"   
    android:groupIndicator="@null"  
    android:id="@+id/mExpandableListView"  
    /&gt;  
&lt;/LinearLayout&gt;  
2、group.xml

 

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout  
 xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
 android:orientation = "horizontal"  
 android:layout\_width="fill\_parent"  
 android:layout\_height="fill\_parent"&gt;  
 &lt;ImageView  
  android:layout\_width = "25dip"  
  android:layout\_height = "25dip"  
  android:layout\_marginTop="10dip"  
  android:id="@+id/mImageView"  
  /&gt;  
 &lt;TextView  
  android:id = "@+id/group\_tv"  
  android:layout\_width = "wrap\_content"  
  android:layout\_height = "wrap\_content"  
  android:paddingLeft = "30px"  
  android:paddingBottom = "10px"  
  android:textSize = "26sp"/&gt;  
&lt;/LinearLayout&gt;

3、child.xml

 

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout  
 xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
 android:orientation = "horizontal"  
 android:layout\_width="match\_parent"  
 android:layout\_height="match\_parent"&gt;  
 &lt;ImageView  
  android:layout\_gravity = "center\_vertical"  
  android:id = "@+id/child\_iv"  
  android:layout\_width = "70px"  
  android:layout\_height = "70px"  
  android:paddingLeft = "30px"  
  android:paddingTop = "2px"  
  android:paddingBottom = "5px"  
  android:src="http://blog.163.com/@drawable/icon"/&gt;  
 &lt;TextView  
  android:layout\_gravity = "center\_vertical"  
  android:id = "@+id/child\_tv"  
  android:layout\_width = "fill\_parent"  
  android:layout\_height = "fill\_parent"  
  android:paddingLeft = "30px"  
  android:paddingTop = "10px"  
  android:paddingBottom = "5px"  
  android:textSize = "30sp"/&gt;  
&lt;/LinearLayout&gt;

4、activity代码

package test.expandableListView;

import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;

import android.app.Activity;  
import android.content.Context;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseExpandableListAdapter;  
import android.widget.ExpandableListView;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.TextView;

public class TestExpandableListView extends Activity  
{  
 @Override  
 public void onCreate(Bundle savedInstanceState)  
 {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  ExpandableListView elv = (ExpandableListView)findViewById(R.id.mExpandableListView);

  //准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2"  
  List&lt;Map&lt;String, String&gt;&gt; groups = new ArrayList&lt;Map&lt;String, String&gt;&gt;();  
  Map&lt;String, String&gt; group1 = new HashMap&lt;String, String&gt;();  
  group1.put("group", "group1");  
  Map&lt;String, String&gt; group2 = new HashMap&lt;String, String&gt;();  
  group2.put("group", "group2");  
  groups.add(group1);  
  groups.add(group2);

  //准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2"  
  List&lt;Map&lt;String, String&gt;&gt; child1 = new ArrayList&lt;Map&lt;String, String&gt;&gt;();  
  Map&lt;String, String&gt; child1Data1 = new HashMap&lt;String, String&gt;();  
  child1Data1.put("child", "child1Data1");  
  Map&lt;String, String&gt; child1Data2 = new HashMap&lt;String, String&gt;();  
  child1Data2.put("child", "child1Data2");  
  child1.add(child1Data1);  
  child1.add(child1Data2);

  //准备第二个一级列表中的二级列表数据:一个二级列表,显示"child2Data1"  
  List&lt;Map&lt;String, String&gt;&gt; child2 = new ArrayList&lt;Map&lt;String, String&gt;&gt;();  
  Map&lt;String, String&gt; child2Data1 = new HashMap&lt;String, String&gt;();  
  child2Data1.put("child", "child2Data1");  
  child2.add(child2Data1);

  //用一个list对象保存所有的二级列表数据  
  List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; childs = new ArrayList&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;();  
  childs.add(child1);  
  childs.add(child2);

  ExpandableAdapter viewAdapter = new ExpandableAdapter(this, groups, childs);  
  elv.setAdapter(viewAdapter);  
 }

 //自定义的ExpandListAdapter  
 class ExpandableAdapter extends BaseExpandableListAdapter  
 {  
  private Context context;  
  List&lt;Map&lt;String, String&gt;&gt; groups;  
  List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; childs;

  /*  
   * 构造函数:  
   * 参数1:context对象  
   * 参数2:一级列表数据源  
   * 参数3:二级列表数据源  
   \*/  
  public ExpandableAdapter(Context context, List&lt;Map&lt;String, String&gt;&gt; groups, List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; childs)  
  {  
   this.groups = groups;  
   this.childs = childs;  
   this.context = context;  
  }

  public Object getChild(int groupPosition, int childPosition)  
  {  
   return childs.get(groupPosition).get(childPosition);  
  }

  public long getChildId(int groupPosition, int childPosition)  
  {  
   return childPosition;  
  }

  //获取二级列表的View对象  
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,  
    ViewGroup parent)  
  {  
   @SuppressWarnings("unchecked")  
   String text = ((Map&lt;String, String&gt;) getChild(groupPosition, childPosition)).get("child");  
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT\_INFLATER\_SERVICE);

   //获取二级列表对应的布局文件, 并将其各元素设置相应的属性  
   LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.child, null);  
   TextView tv = (TextView) linearLayout.findViewById(R.id.child\_tv);  
   tv.setText(text);  
   ImageView imageView = (ImageView)linearLayout.findViewById(R.id.child\_iv);  
   imageView.setImageResource(R.drawable.icon);

   return linearLayout;  
  }

  public int getChildrenCount(int groupPosition)  
  {  
   return childs.get(groupPosition).size();  
  }

  public Object getGroup(int groupPosition)  
  {  
   return groups.get(groupPosition);  
  }

  public int getGroupCount()  
  {  
   return groups.size();  
  }

  public long getGroupId(int groupPosition)  
  {  
   return groupPosition;  
  }

  //获取一级列表View对象  
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)  
  {  
   String text = groups.get(groupPosition).get("group");  
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT\_INFLATER\_SERVICE);

   //获取一级列表布局文件,设置相应元素属性  
   LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);  
   TextView textView = (TextView)linearLayout.findViewById(R.id.group\_tv);  
   textView.setText(text);  
     
   ImageView imageView = (ImageView) linearLayout.findViewById(R.id.mImageView);  
     
   if(getChildrenCount(groupPosition) == 0){//该组下没有子项  
    imageView.setVisibility(View.GONE);  
   }else{  
    if(isExpanded == true){//展开状态  
     imageView.setImageResource(R.drawable.xiala);  
    }else{//收起状态  
     imageView.setImageResource(R.drawable.shouqi);  
    }  
   }

   return linearLayout;  
  }

  public boolean hasStableIds()  
  {  
   return false;  
  }

  public boolean isChildSelectable(int groupPosition, int childPosition)  
  {  
   return false;  
  }

 }  
}

 

截图：

![ExpandableListView修改group图标 - 无尘 - 无尘的博客](http://img.ph.126.net/kMwuogpCr85-MfuRFYqp3g==/634444597522724294.png)

转载自：[http://linglong117.blog.163.com/blog/static/27714547201302433845233/?latestBlog](http://linglong117.blog.163.com/blog/static/27714547201302433845233/?latestBlog)