ExpandableListView 动态加载 点击一级菜单时再加载二级菜单的内容实现
Posted huzhigenlaohu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExpandableListView 动态加载 点击一级菜单时再加载二级菜单的内容实现相关的知识,希望对你有一定的参考价值。
1、加载一级菜单数据viewAdapter = new ExpandableAdapter(this, groupData);
elv.setAdapter(viewAdapter);
2、响应加载二级菜单数据的监听
elv.setOnGroupClickListener(new OnGroupClickListener()
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
List<ClassListItem> childData = new ArrayList<ClassListItem>();
String gradeid=(String) mGradeData.get(groupPosition).get("id");
getChildData(gradeid,childData); //获取子菜单数据
groupData.get(groupPosition).addchilddata(childData); //加载子菜单数据
if (groupData.get(groupPosition).checked)
for (int j=0;j<groupData.get(groupPosition).children.size();j++)
groupData.get(groupPosition).children.get(j).checked=true;
viewAdapter.notifyDataSetChanged();
return false;
);
3、加载获取子菜单数据
private void getChildData(String gradeid,List<ClassListItem> childData)
mClassData=getGradeClassData(gradeid);
for (int j = 0; j < mClassData.size(); j++)
String classid= (String) mClassData.get(j).get("id");
String classname=(String) mClassData.get(j).get("name");
ClassListItem item = new ClassListItem(classname,classid, false);
childData.add(item);
4、定义主菜单类
private class GradeGroup
public String id,name;
private boolean checked;
public List<ClassListItem> children;
public GradeGroup(String gradename,String _gradeid,boolean checked,List<ClassListItem> children)
this.name=gradename;
setChecked(checked);
// this.children=children;
this.id=_gradeid;
addchilddata(children);
public boolean getChecked()
return checked;
public void addchilddata(List<ClassListItem> children)
this.children=children;
public void setChecked(boolean b)
checked=b;
if(children!=null&&children.size()>0)//若children不为空,循环设置children的checked
for(ClassListItem each : children)
each.checked=checked;
5、定义子菜单类
private class ClassListItem
public String id,name;
public boolean checked;
public ClassListItem(String _name,String _classid,boolean _checked)
name=_name;
id=_classid;
checked=_checked;
以上是关于ExpandableListView 动态加载 点击一级菜单时再加载二级菜单的内容实现的主要内容,如果未能解决你的问题,请参考以下文章
我可以在 android 中将 Spannable 转换为 String 以加载 ExpandableListView 吗?
关于android中expandablelistview添加数据的办法
Android ExpandableListView - 寻找教程[关闭]
ExpandableListView 内的 ExpandableListView