在我的ListView(可扩展)中,如果选择了“父母A”的孩子,那么还将同时选择某个父母中的一个孩子。有时似乎有一种模式。
我似乎无法弄清楚这种奇怪的行为。在下面,GymFaciAll是子项,而GymFaci是父项的标题。
public class GymFaciAdapter extends BaseExpandableListAdapter {
Context context;
List<String> selItems;
HashMap<String, List<String>> GymFaciAll;
List<String> GymFaci;
boolean state =false;
public GymFaciAdapter(Context context,
List<String> GymFaci, HashMap<String, List<String>> GymFaciAll){
this.context = context;
this.GymFaciAll = GymFaciAll;
this.GymFaci = GymFaci;
selItems = new ArrayList<>();
}
@Override
public int getGroupCount() {
return GymFaci.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return GymFaciAll.get(GymFaci.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return GymFaci.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return GymFaciAll.get(GymFaci.get(groupPosition)).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 false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String group_title = (String) getGroup(groupPosition);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.exp_parent, parent, false);
}
TextView parentTv = convertView.findViewById(R.id.parent_txt);
parentTv.setTypeface(null, Typeface.BOLD);
parentTv.setText(group_title);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
final String child_title = (String) getChild(groupPosition, childPosition);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.exp_child, parent, false);
}
CheckBox childCb = convertView.findViewById(R.id.checkbox_child);
childCb.setText(child_title);
childCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
selItems.add(child_title);
state = true;
}
else
if(state){
selItems.remove(child_title);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public List<String> getSelItems() {
return selItems;
}
}
应与其他父母的其他孩子无关地检查和取消检查孩子项目。