public class DiagLogExpandableAdapter extends BaseExpandableListAdapter { private Context mContext; private List<GroupInfo> mGroups; public DiagLogExpandableAdapter(Context mContext, List<GroupInfo> mGroups) { this.mContext = mContext; this.mGroups = mGroups; } @Override public int getGroupCount() { return mGroups.size(); } @Override public int getChildrenCount(int groupPosition) { List<ChildInfo> childInfos = mGroups.get(groupPosition).getChildrens(); return childInfos.size(); } @Override public Object getGroup(int groupPosition) { return mGroups.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { GroupInfo groupInfo = mGroups.get(groupPosition); List<ChildInfo> childInfos = groupInfo.getChildrens(); return childInfos.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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(mContext, R.layout.item_diag_log_parent, null); } GroupInfo groupInfo = mGroups.get(groupPosition); CheckBox cb_parent = (CheckBox) convertView.findViewById(R.id.cb_parent); cb_parent.setChecked(groupInfo.isChecked()); cb_parent.setOnClickListener(new OnGroupClickListener(groupPosition)); TextView tv = (TextView) convertView .findViewById(R.id.tv_time); tv.setText(mGroups.get(groupPosition).getTitle()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(mContext, R.layout.item_diag_log_children, null); } CheckBox cb_child = (CheckBox) convertView.findViewById(R.id.cb_child); cb_child.setOnClickListener(new OnChildClickListener(groupPosition, childPosition)); ChildInfo childInfo = mGroups.get(groupPosition).getChildrens().get(childPosition); cb_child.setChecked(childInfo.isChecked()); TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title); tv_title.setText(childInfo.getTitle()); ImageView iv_state = (ImageView) convertView.findViewById(R.id.iv_state); TextView tv_carName = (TextView) convertView.findViewById(R.id.tv_carName); TextView tv_time = (TextView) convertView.findViewById(R.id.tv_time); tv_carName.setText(childInfo.getCarName()); tv_time.setText(childInfo.getFormatTime()); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class OnGroupClickListener implements View.OnClickListener { private int mGroupPosition; public OnGroupClickListener(int groupPosition) { mGroupPosition = groupPosition; } @Override public void onClick(View v) { GroupInfo groupInfo = mGroups.get(mGroupPosition); groupInfo.toggle(); List<ChildInfo> childrens = groupInfo.getChildrens(); for (int i = 0; i < childrens.size(); i++) { ChildInfo childInfo = childrens.get(i); childInfo.setIsChecked(groupInfo.isChecked()); } notifyDataSetChanged(); } } class OnChildClickListener implements View.OnClickListener { private int mGroupPosition; private int mChildPosition; public OnChildClickListener(int mGroupPosition, int mChildPosition) { this.mGroupPosition = mGroupPosition; this.mChildPosition = mChildPosition; } @Override public void onClick(View v) { mGroups.get(mGroupPosition).getChildrens().get(mChildPosition).toggle(); // 检查 Child CheckBox 是否有全部勾选,以控制 Group CheckBox List<ChildInfo> childrens = mGroups.get(mGroupPosition).getChildrens(); boolean childrenAllIsChecked = true; for (int i = 0; i < childrens.size(); i++) { if (!childrens.get(i).isChecked()) childrenAllIsChecked = false; } mGroups.get(mGroupPosition).setIsChecked(childrenAllIsChecked); // 注意,一定要通知 ExpandableListView 資料已經改變,ExpandableListView 會重新產生畫面 notifyDataSetChanged(); } } }
public class GroupInfo { private boolean isChecked; private String title; private List<ChildInfo> childrens; private String folderName; public GroupInfo(boolean isChecked, String title, List<ChildInfo> childrens, String folderName) { this.isChecked = isChecked; this.title = title; this.childrens = childrens; this.folderName = folderName; } public boolean isChecked() { return isChecked; } public void setIsChecked(boolean isChecked) { this.isChecked = isChecked; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<ChildInfo> getChildrens() { return childrens; } public void setChildren(List<ChildInfo> childrens) { this.childrens = childrens; } public String getFolderName() { return folderName; } public void setFolderName(String folderName) { this.folderName = folderName; } @Override public String toString() { return "GroupInfo{" + "isChecked=" + isChecked + ", title='" + title + '\'' + ", childrens=" + childrens + ", folderName='" + folderName + '\'' + '}'; } public void toggle() { this.isChecked = !this.isChecked; } }public class ChildInfo implements Comparable{ private boolean isChecked; private String title; private int sendState; private String carName; private String formatTime; /* * xml文件所描述的txt文件的路径 * */ private String filePath; private String xmlFileName; public ChildInfo(boolean isChecked, String title, int sendState, String carName, String formatTime, String filePath, String xmlFileName) { this.isChecked = isChecked; this.title = title; this.sendState = sendState; this.carName = carName; this.formatTime = formatTime; this.filePath = filePath; this.xmlFileName = xmlFileName; } public boolean isChecked() { return isChecked; } public void setIsChecked(boolean isChecked) { this.isChecked = isChecked; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getSendState() { return sendState; } public void setSendState(int sendState) { this.sendState = sendState; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public String getFormatTime() { return formatTime; } public void setFormatTime(String formatTime) { this.formatTime = formatTime; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getXmlFileName() { return xmlFileName; } public void setXmlFileName(String xmlFileName) { this.xmlFileName = xmlFileName; } @Override public String toString() { return "ChildInfo{" + "isChecked=" + isChecked + ", title='" + title + '\'' + ", sendState=" + sendState + ", carName='" + carName + '\'' + ", formatTime='" + formatTime + '\'' + ", filePath='" + filePath + '\'' + ", xmlFileName='" + xmlFileName + '\'' + '}'; } public void toggle() { this.isChecked = !this.isChecked; } @Override public int compareTo(Object another) { ChildInfo another1 = (ChildInfo) another; return this.formatTime.compareTo(another1.getFormatTime()); } }