我希望能够创建一个包含父级和子级复选框的多级列表视图。
我已经能够使用ExpandableListAdapter获取工作列表视图,但是我无法为复选框创建一种通信方式。
我的愿望是,当选中父复选框时,所有子复选框都将被选中,当取消选中时,相反。此外,如果未选中父复选框并且选择了子项,则应选中父复选框。
有很多关于单层复选框的教程,但我还没有找到一篇描述我上面所说的内容。
非常感谢任何帮助!
这是我的ExtendableListAdapter类:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
private List<Boolean> _listDataBoolean; // header checkbox values
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private List<ArrayList<Boolean>> _listChildBoolean;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
List<Boolean> listDataBoolean,
List<ArrayList<Boolean>> listChildBoolean) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listDataBoolean = listDataBoolean;
this._listChildBoolean = listChildBoolean;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
CheckBox chk = (CheckBox) convertView
.findViewById(R.id.lblChildCheckbox);
chk.setChecked(_listChildBoolean.get(groupPosition).get(childPosition));
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
Boolean bool = _listDataBoolean.get(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
CheckBox chk2 = (CheckBox) convertView
.findViewById(R.id.lblHeaderCheckbox);
chk2.setChecked(bool);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 0 :(得分:0)
您必须将OnCheckChangeListeners添加到复选框。