我有一个可扩展的列表视图。在子项中,这是gridview。在gridview内部有自定义复选框。
现在的问题是,当我选中一个复选框并向下滚动,甚至关闭父视图时,检查已经消失。
这样的事情:
滚动前检查屏幕:
滚动后检查屏幕:
这是我的 ExpandableListView适配器:
public class CreateProfileExpandableAdapter extends BaseExpandableListAdapter {
ArrayList<CreateProfileFecetsHeaderModel> al;
Activity activity;
public CreateProfileExpandableAdapter(Activity activity, ArrayList<CreateProfileFecetsHeaderModel> al) {
this.activity = activity;
this.al = al;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return al.get(groupPosition).getChildren();
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return al.get(groupPosition).hashCode();
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.create_profile_child, parent, false);
}
ExpandableHeightGridView gridViewChildren = (ExpandableHeightGridView) v.findViewById(R.id.gridViewChildren);
gridViewChildren.setExpanded(true);
CreateProfileChildAdapter createProfileChildAdapter = new CreateProfileChildAdapter(activity, al.get(groupPosition).getChildren());
gridViewChildren.setAdapter(createProfileChildAdapter);
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return al.get(groupPosition);
}
@Override
public int getGroupCount() {
return al.size();
}
@Override
public long getGroupId(int groupPosition) {
return al.get(groupPosition).hashCode();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.create_profile_group, parent, false);
}
TextView textViewGroup = (TextView) v.findViewById(R.id.textViewGroup);
textViewGroup.setText(al.get(groupPosition).getText());
return v;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Gridview适配器:
public class CreateProfileChildAdapter extends BaseAdapter {
Activity activity;
ArrayList<CreateProfileFecetsChildModel> al;
int index = -1;
public CreateProfileChildAdapter(Activity activity, ArrayList<CreateProfileFecetsChildModel> al) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.al = al;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
private static class ViewHolder {
CheckBox checkBoxChild;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
final int pos = position;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(R.layout.create_profile_grid_child, parent, false);
viewHolder = new ViewHolder();
viewHolder.checkBoxChild = (CheckBox) convertView.findViewById(R.id.checkBoxChild);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBoxChild.setText(al.get(position).getTitle());
viewHolder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.white));
} else {
viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.black));
}
}
});
return convertView;
}
}
答案 0 :(得分:2)
问题是视图在没有重置状态的情况下被回收。想象一下,在屏幕上显示20个项目,一次只显示四个项目,这意味着GridViewAdapter使用四个,也许五个视图。当滚动列表时,这些视图轮流显示您的项目:
<强>解决方案:强>
CreateProfileFecetsHeaderModel
类型的子列表条目中存储有关视图状态的所有内容(在您的情况下:标记为已选中的内容)。getChildView()
中使用position
从适配器的子项中获取已检查状态,并相应地设置持有者的CheckBox。答案 1 :(得分:0)
由于它是可扩展适配器,你需要知道groupid,selectedchild,id。 尝试如下......
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Pair<Long, Long> tag = new Pair<Long, Long>(
getGroupId(groupPosition),
getChildId(groupPosition, childPosition));
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_countryoforigin, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBox.setTag(tag);
viewHolder.checkBox.setChecked(mCheckedItems.contains(tag));
viewHolder.textview.setText((CharSequence) getText(groupPosition, childPosition));
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final Pair<Long, Long> tag = (Pair<Long, Long>) v.getTag();
if (cb.isChecked()) {
mCheckedItems.add(tag);
} else {
mCheckedItems.remove(tag);
}
}
});
return convertView;
}
public Set<Pair<Long, Long>> getCheckedItems() {
return mCheckedItems;
}
你可以打电话给getcheckitems .....