ExpandListView实现下拉列表案例
使用方式与ListView类似,是ListView的一个延申,Group为TextView,子元素为ListView。
效果图:
代码:
实体类:
Group.java
public class Group {
private String gName;
public Group() {
}
public Group(String gName) {
this.gName = gName;
}
public String getName() {
return gName;
}
public void setName(String gName) {
this.gName = gName;
}
}
Item.java
public class Item {
private int iId;
private String iName;
public Item() {
}
public Item(int iId, String iName) {
this.iId = iId;
this.iName = iName;
}
public int getId() {
return iId;
}
public String getName() {
return iName;
}
public void setId(int iId) {
this.iId = iId;
}
public void setName(String iName) {
this.iName = iName;
}
}
自定义适配器:ExpandableListViewAdapter.java
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<Group> groupList;
private ArrayList<ArrayList<Item>> itemList;
public ExpandableListViewAdapter(Context context, ArrayList<Group> gData, ArrayList<ArrayList<Item>> iData) {
this.mContext = context;
this.groupList = gData;
this.itemList = iData;
}
//返回Group的个数
@Override
public int getGroupCount() {
return groupList.size();
}
//返回某个Group对应的Item的个数
@Override
public int getChildrenCount(int groupPosition) {
return itemList.get(groupPosition).size();
}
//返回某个Group对象
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);