设置事件监听
去除分割线android:divider="@null"
expandableListView = (ExpandableListView) findViewById(R.id.expandable_list);
//remove group icon
expandableListView.setGroupIndicator(null);
//group click
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//return false,else not expand
return false;
}
});
//child click
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
childClickHandler(groupPosition,childPosition);
return true;
}
});
适配器
默认折叠图标在左边,可以通过自定义设置图标的位置
public class MyExpandableListAdapter extends BaseExpandableListAdapter{
@Override
public int getGroupCount() {
return buildingInfo.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return buildingInfo.get(groupPosition).BuildingList.size();
}
@Override
public Object getGroup(int groupPosition) {
return buildingInfo.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return buildingInfo.get(groupPosition).BuildingList;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_group, parent, false);
groupViewHolder = new GroupViewHolder();
groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.group_label);
groupViewHolder.tvTitle.setTypeface(typeface);
groupViewHolder.imageView = (ImageView) convertView.findViewById(R.id.group_indicator);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.tvTitle.setText(buildingInfo.get(groupPosition).Office);
if(isExpanded){
groupViewHolder.imageView.setImageResource(R.drawable.icon_up);
}else{
groupViewHolder.imageView.setImageResource(R.drawable.icon_down);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_child, parent, false);
childViewHolder = new ChildViewHolder();
childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_expand_child);
childViewHolder.tvTitle.setTypeface(typeface);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.tvTitle.setText(buildingInfo.get(groupPosition).BuildingList.get(childPosition).Building);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class GroupViewHolder {
TextView tvTitle;
ImageView imageView;
}
private class ChildViewHolder {
TextView tvTitle;
}
}
expandabel