参考自http://blog.youkuaiyun.com/sysukehan/article/details/51960473
先用LinearLayout作为父类,包裹在ExpandableListView外边。,命名为:newlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="@+id/expandable"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
再Activity中引用,
setContentView(R.layout.newlayout);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandable);
private Map<String, List<String>> map;
map = new HashMap<>();
创建第一个泛型:
father = new String[]{"one","two","three"};
创建第二个泛型:
ArrayList<String> child1 = new ArrayList<>();
ArrayList<String> child2 = new ArrayList<>();
ArrayList<String> child3 = new ArrayList<>();
child1.add(father[0]+"-"+"first");
child1.add(father[1]+"-"+"two");
child1.add(father[2]+"-"+"three");
child2.add(father[0]+"-"+"first");
child2.add(father[1]+"-"+"two");
child2.add(father[2]+"-"+"three");
child3.add(father[0]+"-"+"first");
child3.add(father[1]+"-"+"two");
child3.add(father[2]+"-"+"three");
map.put(father[0],child1);
map.put(father[1],child2);
map.put(father[2],child3);
先写折叠的父类布局:item_bolt_attribute_lv0.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/tv_attribute_title"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17.5dp"
android:text="功效"
/>
<TextView
android:id="@+id/tv_attribute_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:drawablePadding="7dp"
android:singleLine="true"
android:text="不限"
android:maxEms="10"
android:textSize="14sp"/>
</RelativeLayout>
再写子类的布局:item_bolt_attribute_lv1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_sub_name"
android:text="不限"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="14sp"
android:layout_marginLeft="46dp"
/>
<CheckBox
android:id="@+id/cb_name"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="59dp"
android:button="@null"
android:checked="false"
android:paddingLeft="10dp"
/>
</RelativeLayout>
然后创建一个类继承自ExpandableListAdapter,重写所有方法:
class MyExpandable extends BaseExpandableListAdapter {
/**
* 获取父类的数量
*
* @return
*/
@Override
public int getGroupCount() {
return map.size();
}
/**
* 获取子类的数量
*
* @param groupPosition
* @return
*/
@Override
public int getChildrenCount(int groupPosition) {
return map.get(father[groupPosition]).size();
}
/**
* 获取某个父类
*
* @param groupPosition
* @return
*/
@Override
public Object getGroup(int groupPosition) {
return map.get(father[groupPosition]);
}
/**
* 获取某个子类
*
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return map.get(father[groupPosition]).get(childPosition);
}
/**
* 获取父类的id
*
* @param groupPosition
* @return
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 获取子类的id
*
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 按函数的名字来理解应该是是否具有稳定的id,这个方法目前一直都是返回false,没有去改动过
*
* @return
*/
@Override
public boolean hasStableIds() {
return false;
}
/**
* 获取父类显示的view
*
* @param groupPosition
* @param isExpanded
* @param convertView
* @param parent
* @return
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//关联布局
View view = View.inflate(getBaseContext(), R.layout.item_bolt_attribute_lv0, null);
TextView name = (TextView) view.findViewById(R.id.tv_attribute_name);
TextView title = (TextView) view.findViewById(R.id.tv_attribute_title);
//设置文字,算作父类的
name.setText(father[groupPosition]);
title.setText(groupPosition + "");
return view;
}
/**
* 获取子类显示的view
*
* @param groupPosition
* @param childPosition
* @param isLastChild
* @param convertView
* @param parent
* @return
*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//关联布局
View view = View.inflate(getBaseContext(), R.layout.item_bolt_attribute_lv1, null);
TextView name = (TextView) view.findViewById(R.id.tv_sub_name);
CheckBox box = (CheckBox) view.findViewById(R.id.cb_name);
//获取子类值
List<String> strings = map.get(father[groupPosition]);
String s = strings.get(childPosition);
name.setText(s);
return view;
}
/**
* 子类是否可以选中,如果需要设置子类的点击事件,返回true
*
* @param groupPosition
* @param childPosition
* @return
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
完成后就是这样:
但是仔细看的话,会发现,前面有个向下的小箭头,在ExpandableListView的布局中有一条属性:
android:groupIndicator="@null"
置为@null就没有了,可以换成其他的图标,直接指定位置就行。
如果想指定放在右边,一个博客中写的:http://blog.youkuaiyun.com/dellheng/article/details/7176101
int width = getWindowManager().getDefaultDisplay().getWidth();
expandableListView.setIndicatorBounds(width-40, width-10);
不过确实管用,但是适配方面要记得做好。
其实可以给每个条目指定一个图标放在右边也可以。