效果图:
代码实现:
- 1,布局中引入
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
-
2,设置所需bean文件
public class GroupBean { private int img; private String txt; private List<ChildBean> list_child = new ArrayList<ChildBean>(); public List<ChildBean> getList_child() { return list_child; } public void setList_child(List<ChildBean> list_child) { this.list_child = list_child; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getTxt() { return txt; } public void setTxt(String txt) { this.txt = txt; } }
public class ChildBean {
private int img;
private String txt;
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
- 3,绑定view和数据
// 准备数据
private List<GroupBean> list_data;
private int[] images = { R.drawable.aa, R.drawable.bb, R.drawable.cc };
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initData();
initView();
}
private void initView() {
ExpandableListView listView = findViewById(R.id.expandableListView);
MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter(this,list_data);
listView.setAdapter(adapter);
}
int[] arr = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};
private void initData() {
list_data = new ArrayList<GroupBean>();
for (int i = 0; i < 5; i++) {
GroupBean group = new GroupBean();
group.setImg(arr[i]);
group.setTxt("组" + (i + 1));
List<ChildBean> child = new ArrayList<ChildBean>();
for (int j = 0; j < 3; j++) {
ChildBean chid = new ChildBean();
chid.setImg(images[j]);
chid.setTxt("子" + (j + 1));
child.add(chid);
}
group.getList_child().addAll(child);
list_data.add(group);
}
}
4,设置适配器
class MyExpandableListViewAdapter extends BaseExpandableListAdapter{
private List<GroupBean> list_data;
private Context context;
public MyExpandableListViewAdapter(Context context, List<GroupBean> list_data) {
this.context = context;
this.list_data = list_data;
}
@Override
public int getGroupCount() {
return list_data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list_data.get(groupPosition).getList_child().size();
}
@Override
public Object getGroup(int groupPosition) {
return list_data.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list_data.get(groupPosition).getList_child().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
HolderGroup group = null;
if (convertView == null) {
convertView = View.inflate(context,R.layout.layout_group,null);
group = new HolderGroup(convertView);
convertView.setTag(group);
} else {
group = (HolderGroup) convertView.getTag();
}
group.img.setImageResource(list_data.get(groupPosition).getImg());
group.tv.setText(list_data.get(groupPosition).getTxt());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
HolderChild child = null;
if (convertView == null) {
convertView = View.inflate(context,R.layout.layout_item,null);
child = new HolderChild(convertView);
convertView.setTag(child);
} else {
child = (HolderChild) convertView.getTag();
}
child.img.setImageResource(list_data.get(groupPosition).getList_child().get(childPosition).getImg());
child.tv.setText(list_data.get(groupPosition).getList_child().get(childPosition).getTxt());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class HolderGroup {
ImageView img;
TextView tv;
public HolderGroup(View convertView) {
img = (ImageView) convertView.findViewById(R.id.iv_group);
tv = (TextView) convertView.findViewById(R.id.tv_group);
}
}
class HolderChild {
ImageView img;
TextView tv;
public HolderChild(View convertView) {
img = (ImageView) convertView.findViewById(R.id.iv_item);
tv = (TextView) convertView.findViewById(R.id.tv_item);
}
}
}