原CSDN博客已弃用,文章会逐渐迁移过来。

组条目布局

item_elv_group.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  

    <TextView  
        android:id="@+id/txt_group_data"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textSize="28sp"  
        android:text="学习"  
        android:textColor="#423f4d"/>  
</LinearLayout>  

子条目布局

item_elv_child.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  

    <TextView  
        android:id="@+id/txt_child_data"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textSize="20sp"  
        android:text="Activity生命周期"  
        android:textColor="#d69842"/>  
</LinearLayout>  

主布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="com.administrator.shoufengqindemo.MainActivity">  

    <ExpandableListView  
        android:id="@+id/elv_local_data"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:groupIndicator="@null">  
    </ExpandableListView>  

</RelativeLayout>  

设置groupIndicator属性去掉ExpandableListView 默认的箭头

用到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下:
settingLists.setGroupIndicator(null); 此处就是设置自定义的箭头图标的。置空则没有了。
也可以自定义但是位置还是在那个地方(不推荐)如下:

首先,自定义一个expandablelistviewselector.xml文件:

<?xml version="1.0" encoding="utf-8"?>     
<selector xmlns:android="http://schemas.android.com/apk/res/android">     
     <item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />     
      <item android:drawable="@drawable/expandablelistviewindicator" />    
 </selector>   <span><span></span><span>   
an></span>  

在Java中设置

settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));     

或xml设置

android:groupIndicator="@drawable/groupIndicator_selector"    

本地数据源

LacalData,java

package com.administrator.shoufengqindemo;  

import java.util.ArrayList;  
import java.util.List;  

/** 
 * Created by Administrator on 2018/5/15. 
 */  

public class LocalData {  
    /** 
     * 获取列表组数据 
     * @return 
     */  
    public static List<String> getGroupData(){  
        List<String> groups = new ArrayList<String>();  
        groups.add("学习");  
        groups.add("看书");  
        return groups;  
    }  

    /** 
     * 获取子条目 
     * @return 
     */  
    public static List<List<String>> getChildData(){  
        List<List<String>> childs = new ArrayList<List<String>>();  
        List<String> item1 = new ArrayList<String>();  
        item1.add("Android属性动画");  
        item1.add("Android Activity生命周期");  
        childs.add(item1);  
        List<String> item2 = new ArrayList<String>();  
        item2.add("面向对象");  
        item2.add("接口");  
        item2.add("集合");  
        childs.add(item2);  
        return childs;  
    }  
}  

装载数据适配器

LocalAdapter.java

package com.administrator.shoufengqindemo;  

/** 
 * Created by Administrator on 2018/5/15. 
 */  

import android.content.Context;  
import android.view.View;  
import android.widget.TextView;  
/* 
本地数据适配器 
 */  
class LocalAdapter extends MyBaseAdapter {  

    public LocalAdapter(Context context) {  
        super(context);  
    }  

    @Override  
    public View MyGroupView(int groupPosition, View groupView) {  
        View view = minInflater.inflate(R.layout.item_elv_group, null);  
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_group_data);  
        txtTitle.setText(getGroup(groupPosition));  
        txtTitle.setPadding(30, 0, 0, 0);  
        return view;  
    }  

    @Override  
    public View MyChildView(int groupPosition, int childPosition, View childView) {  
        View cView = minInflater.inflate(R.layout.item_elv_child, null);  
        TextView txtContent = (TextView) cView.findViewById(R.id.txt_child_data);  
        txtContent.setText(getChild(groupPosition,childPosition));  
        txtContent.setPadding(30, 0, 0, 0);  
        return cView;  
    }  


}  

视图适配器

MyBaseAdapter.java

package com.administrator.shoufengqindemo;  

import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseExpandableListAdapter;  

import java.util.List;  

/** 
 * Created by Administrator on 2018/5/15. 
 */  

public abstract class MyBaseAdapter extends BaseExpandableListAdapter {  
    Context mContext;  
    LayoutInflater minInflater;  
    private List<String> groups;  
    private List<List<String>> childs;  
    public MyBaseAdapter(Context context){  
        mContext=context;  
        minInflater = LayoutInflater.from(mContext);  
    }  

    public void addNewData(List<String> groups,List<List<String>> childs){  
        this.groups = groups;  
        this.childs = childs;  
    }  
    /** 
     *获取组的数量 
     */  
    @Override  
    public int getGroupCount() {  
        return groups.size();  
    }  

    /** 
     * 获取组的具体内容 
     */  
    @Override  
    public String getGroup(int groupPosition) {  
        return groups.get(groupPosition);  
    }  

    /** 
     * 获取组的id 
     */  
    @Override  
    public long getGroupId(int groupPosition) {  
        return 0;  
    }  

    /** 
     * 获取组的视图 
     */  
    @Override  
    public View getGroupView(int groupPosition, boolean isExpanded, View groupView, ViewGroup parent) {  
        return MyGroupView(groupPosition,groupView);  
    }  

    /** 
     * 获取子条目数量 
     */  
    @Override  
    public int getChildrenCount(int groupPosition) {  
        return childs.get(groupPosition).size();  
    }  

    /** 
     * 获取具体的子条目 
     */  
    @Override  
    public String getChild(int groupPosition, int childPosition) {  
        return childs.get(groupPosition).get(childPosition);  
    }  

    /** 
     * 获取子条目的视图 
     */  
    @Override  
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
        return MyChildView(groupPosition,childPosition,convertView);  
    }  

    public abstract View MyGroupView(int groupPosition,  View grounpView);  
    public abstract View MyChildView(int groupPosition, int childPosition , View childView);  

    /** 
     * 获取子条目的id 
     */  
    @Override  
    public long getChildId(int groupPosition, int childPosition) {  
        return 0;  
    }  

    @Override  
    public boolean hasStableIds() {  
        return false;  
    }  

    /** 
     * 子集是否可选,true为可选,如子条目需要设置点击事件,则返回值应为true 
     * @param groupPosition 
     * @param childPosition 
     * @return 
     */  
    @Override  
    public boolean isChildSelectable(int groupPosition, int childPosition) {  
        return true;  
    }  
}  

主调用界面

MainActivity.java

package com.administrator.shoufengqindemo;  

import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ExpandableListView;  
import android.widget.Toast;  

public class MainActivity extends Activity {  

    private ExpandableListView mElv;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        mElv = (ExpandableListView) this.findViewById(R.id.elv_local_data);  

        //第二种方式获取对象  
        //MainActivity继承ExpandableListActivity  
//        mElv = this.getExpandableListView();  
        loadLocal();  


        /** 
         *设置组的点击事件 
         */  
        mElv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {  
            @Override  
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {  
                Toast.makeText(MainActivity.this,"您点击的是:"+LocalData.getGroupData().get(groupPosition),Toast.LENGTH_SHORT).show();  
                return false;  
            }  
        });  

        /** 
         * 设置子条目的点击事件 
         * 注意,设置子条目点击事件时,应到适配器MyBaseAdapter中观察 
         * isChildSelectable()方法设置的子集是否可选,可选应返回true 
         */  
        mElv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {  
            @Override  
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {  
                Toast.makeText(MainActivity.this, "您点击的是:" + LocalData.getChildData().get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show();  
                return false;  
            }  
        });  
    }  

    /** 
     *   加载本地数据 
     */  
    public void loadLocal(){  
        LocalAdapter adapter = new LocalAdapter(this);  
        //添加数据  
        adapter.addNewData(LocalData.getGroupData(),LocalData.getChildData());  

        mElv.setAdapter(adapter);  
    }  
}  

效果如图: