1.在activity_main.xml文件中定义ExpandableListView控件
2.定义组视图layout_group.xml
3.定义子视图layout_child.xml
4.在MainActivity中的代码
private ExpandableListView lv;
public String[] groupArray = new String[]{"武侠类", "惊悚/恐怖类", "影视小说"};
public String[][] childArray = new String[][]{
{"天龙八部", "倚天屠龙记", "神雕侠侣", "鹿鼎记"},
{"盗墓笔记", "摸金校尉", "鬼吹灯"},
{"花千骨", "步步惊心", "琅琊榜", "追风筝的人"}};
public int[][] imgs = {
{R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round},
{R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round},
{R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round,R.mipmap.ic_launcher_round}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
lv = (ExpandableListView) findViewById(R.id.elv);
MyExpandListViewAdapter adapter = new MyExpandListViewAdapter(MainActivity.this, groupArray, childArray, imgs);
lv.setAdapter(adapter);
//二级列表子条目点击事件
lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this,"你点的是:"+childArray[groupPosition][childPosition],Toast.LENGTH_SHORT).show();
return false;
}
});
}
5.适配器
public class MyExpandListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private String[] groupArray;
private String[][] childArray;
private int[][] imgs;
public MyExpandListViewAdapter(Context context, String[] groupArray, String[][] childArray, int[][] imgs) {
this.context = context;
this.groupArray = groupArray;
this.childArray = childArray;
this.imgs = imgs;
}
@Override
public int getGroupCount() {
return groupArray.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return childArray[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@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) {
View view = LayoutInflater.from(context).inflate( R.layout.layout_group, null);
TextView groupName = view.findViewById(R.id.tv_group);
groupName.setText(groupArray[groupPosition]);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_child,null);
TextView tv_name = view.findViewById(R.id.tv_name);
ImageView img = view.findViewById(R.id.img);
tv_name.setText(childArray[groupPosition][childPosition]);
img.setImageResource(imgs[groupPosition][childPosition]);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}