尝试使用getChildAt()取消选择视图

我在ExpandableListView使用GridView

我有一个function,当它被点击时突出显示一个项目,现在我正在尝试实现一个按钮,当按下它时将取消选择所有选定的项目,但它只取消选择最后一个查看的视图

 public class GridAdapter extends BaseAdapter { private Context mContext; private ArrayList child; public ArrayList escola; private ArrayList<ArrayList> filhos = new ArrayList(); public GridAdapter(Context context, ArrayList groups, ArrayList childValues, int groupPosition) { mContext = context; child = childValues; escola = groups; posicaoEscola = groupPosition; } @Override public int getCount() { return child.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(final int position, View convertView, final ViewGroup parent) { holder = null; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.child_item, null); holder = new ViewHolder(); final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca); final TextView nomeAluno = (TextView) convertView.findViewById(R.id.name); convertView.setTag(holder); final View finalConvertView = convertView; convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (list.size() > 0) { isChecked = filhos.get(posicaoEscola).get(position).isChecked(); if (!isChecked) { selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE); } else { selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE); } ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE); } }); } }); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText(child.get(position).getNome()); return convertView; } static class ViewHolder { TextView text; } public void selecao(boolean check, int position, TextView nomeAluno, int color, View v, int visibility) { filhos.get(posicaoEscola).get(position).setChecked(check); nomeAluno.setTextColor(color); v.findViewById(R.id.overlay).setVisibility(visibility); v.findViewById(R.id.overlayText).setVisibility(visibility); } public void limpaSelecao(boolean check, int position, TextView nomeAluno, int color, ViewGroup v, int visibility) { for (int x = 0; x < group.size(); x++) { for (int j = 0; j < group.get(x).getalunos().size(); j++) { if(filhos.get(x).get(j).isChecked()){ v.getChildAt(j).findViewById(R.id.loadingPanel).findViewById(R.id.overlay).setVisibility(View.GONE); } nomeAluno.setTextColor(color); } } } } 

布局:

    

这是正在发生的事情:

当我选择A到B中的所有项目时:

在此处输入图像描述

并且清除,它只会从我点击的最后一组中删除所选的孩子:

在此处输入图像描述

当我点击Clear时,我需要将它们全部删除,所有的孩子无论在哪个组中都必须被删除。

ExpandableAdapter:

 public class ExpandListTest extends BaseExpandableListAdapter { public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE; public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL; /** * No child could be selected */ public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE; /** * One single choice per group */ public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE; /** * One single choice for all the groups */ public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001; private Context context; public static ArrayList groups; private ArrayList<ArrayList> child = new ArrayList(); private HashMap aMap = new HashMap(); private HashMap HashTask = new HashMap(); public static ArrayList listchild; private GridAdapter adapter; private SecurePreferences Sessao; public static CustomGridView gridView; private SparseArray checkedPositions; private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName(); public ExpandListTest(Context context, ArrayList groups, HashMap data, SecurePreferences mSessao, HashMap hashTask) { this.context = context; this.groups = groups; this.aMap = data; this.Sessao = mSessao; checkedPositions = new SparseArray(); child = new ArrayList(); if (groups != null) { for (int i = 0; i < groups.size(); i++) { child.add(i, groups.get(i).getalunos()); } } } @Override public Object getChild(int groupPosition, int childPosition) { return child.get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) { if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) context .getSystemService(context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.gridview, null); } listchild = new ArrayList(); for (int j = 0; j < groups.get(groupPosition).getalunos().size(); j++) { listchild.add(child.get(groupPosition).get(j)); } gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar); gridView.setExpanded(true); adapter = new GridAdapter(context, groups, listchild, child, groupPosition, aMap, HashTask, Sessao); gridView.setAdapter(adapter);// Adapter gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE); return convertView; } @Override public int getChildrenCount(int nGroup) { return 1; } @Override public Object getGroup(int groupPosition) { return groups.get(groupPosition); } @Override public int getGroupCount() { return groups.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { CadastraEscolas group = (CadastraEscolas) getGroup(groupPosition); if (convertView == null) { LayoutInflater inf = (LayoutInflater) context .getSystemService(context.LAYOUT_INFLATER_SERVICE); convertView = inf.inflate(R.layout.group_item, null); } ExpandableListView mExpandableListView = (ExpandableListView) parent; mExpandableListView.expandGroup(groupPosition); TextView tv = (TextView) convertView.findViewById(R.id.group_name); tv.setText(group.getNome_fantasia()); return convertView; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } 

Android ViewGroups可以包含任意数量的视图,但是视图只能有一个父ViewGroup,如果您尝试添加已经拥有父视图的View,您将获得此exception(来自ViewGroup.addViewInner()):

 throw new IllegalStateException("The specified child already has a parent. " + "You must call removeView() on the child's parent first."); 

这意味着布局的结构是树的结构(而不​​是图形),因此您可以使用任何树遍历算法迭代布局的每个视图。

以下是可能的算法之一,即后序遍历。

在此处输入图像描述

它遍历根元素,获取第一个子节点并对算法进行递归调用,然后是第二个,第三个等等……当没有子节点通过时,它会调用节点的取消选择函数。

对于图中的树,将按以下顺序取消选择节点:

A,C,E,D,B,H,I,G,F

 public void onClickTheButton(View view) { unselectall(R.layout.your_layout); } public void unselectAll(View view) { if(view instanceof ViewGroup) { for(int ii = 0 ; ii< (ViewGroup)view.getChildrenCount(); ii++) { unselectAll((ViewGroup)view.getChildAt(ii)); } } unselect(view); } 

您可以在此处find许多其他方法: https : //en.wikipedia.org/wiki/Tree_traversal

如果您想要简化编程体验,那么必须了解这些算法的工作原理。

更改你的getView()方法如下。

 @Override public View getView(final int position, View convertView, final ViewGroup parent) { holder = null; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.child_item, null); holder = new ViewHolder(); final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText(child.get(position).getNome()); final View finalConvertView = convertView; convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (list.size() > 0) { isChecked = filhos.get(posicaoEscola).get(position).isChecked(); if (!isChecked) { selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE); } else { selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE); } ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE); } }); } }); return convertView; }