当单击可扩展的 ListView 然后选中 Checkbox 时自动取消选中并选中 Last CheckBox
When click on expandable ListView then checked Checkbox is automatically unchecked and Last CheckBox is checked
Screnshot of 当我检查 ExpandableListView
中的前三个 CheckBoxes
并从中展开第一个或第二个项目 然后第二个 CheckBox
自动 未选中 和最后一个 CheckBox
自动 Checked
这是它的Screenshot。
这是我的自定义适配器代码
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<Group_Items> group_al;
public MyBaseExpandableListAdapter(Context context,ArrayList<Group_Items> group_al) {
this.context=context;
this.group_al=group_al;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child_Items> chList = group_al.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
Child_Items ch = (Child_Items) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_items, null);
}
TextView child = (TextView) convertView.findViewById(R.id.child);
TextView dd = (TextView) convertView.findViewById(R.id.dd);
TextView date= (TextView) convertView.findViewById(R.id.date);
child.setText(ch.getChild_title().toString());
dd.setText(ch.getDd().toString());
date.setText(ch.getDate().toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child_Items> chList = group_al.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return group_al.get(groupPosition);
}
@Override
public int getGroupCount() {
return group_al.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Group_Items gr = (Group_Items) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.expandable_group_items, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
你可以使用 SparseBooleanArray 来完成这个
在您的适配器中,初始化并保留一个稀疏布尔数组
SparseBooleanArray mSelections = new SparseBooleanArray();
在您的 getGroupView
中,执行以下操作
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group_Items gr = (Group_Items) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.expandable_group_items, null);
// adding onClick listener on CheckBox for the very first time
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
mSelections.put(position, !mSelections.get(position, false));
((CheckBox)v).setChecked(mSelections.get(position, false));
}
});
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setTag(groupPosition);
// reassigning checkbox to its ticked state
chk.setChecked(mSelections.get(groupPosition, false));
return convertView;
}
Screnshot of 当我检查 ExpandableListView
中的前三个 CheckBoxes
并从中展开第一个或第二个项目 然后第二个 CheckBox
自动 未选中 和最后一个 CheckBox
自动 Checked
这是它的Screenshot。
这是我的自定义适配器代码
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<Group_Items> group_al;
public MyBaseExpandableListAdapter(Context context,ArrayList<Group_Items> group_al) {
this.context=context;
this.group_al=group_al;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child_Items> chList = group_al.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
Child_Items ch = (Child_Items) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_items, null);
}
TextView child = (TextView) convertView.findViewById(R.id.child);
TextView dd = (TextView) convertView.findViewById(R.id.dd);
TextView date= (TextView) convertView.findViewById(R.id.date);
child.setText(ch.getChild_title().toString());
dd.setText(ch.getDd().toString());
date.setText(ch.getDate().toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child_Items> chList = group_al.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return group_al.get(groupPosition);
}
@Override
public int getGroupCount() {
return group_al.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Group_Items gr = (Group_Items) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.expandable_group_items, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
你可以使用 SparseBooleanArray 来完成这个
在您的适配器中,初始化并保留一个稀疏布尔数组
SparseBooleanArray mSelections = new SparseBooleanArray();
在您的 getGroupView
中,执行以下操作
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group_Items gr = (Group_Items) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.expandable_group_items, null);
// adding onClick listener on CheckBox for the very first time
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
mSelections.put(position, !mSelections.get(position, false));
((CheckBox)v).setChecked(mSelections.get(position, false));
}
});
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setTag(groupPosition);
// reassigning checkbox to its ticked state
chk.setChecked(mSelections.get(groupPosition, false));
return convertView;
}