java - Trying to deselect a view using getChildAt() -
i'm using gridview
inside expandablelistview
i have function highlights item when clicked, i'm trying implement button when pressed unselect selected items, unselecting last view clicked
public class gridadapter extends baseadapter { private context mcontext; private arraylist<filhos> child; public arraylist<cadastraescolas> escola; private arraylist<arraylist<filhos>> filhos = new arraylist(); public gridadapter(context context, arraylist<cadastraescolas> groups, arraylist<filhos> 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) { (int x = 0; x < group.size(); x++) { (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); } } } }
layout:
<relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/nomealuno_main" android:layout_below="@+id/child"> <view android:id="@+id/overlay" android:layout_width="125dp" android:layout_height="50dp" android:background="@color/bgoverlay" android:visibility="gone"/> </relativelayout>
here happening:
when select items b that:
and hit clear, remove selected children last group clicked:
i need remove them when click on clear, children no matter in group have removed.
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 selected */ public static final int choice_mode_none = abslistview.choice_mode_none; /** * 1 single choice per group */ public static final int choice_mode_single_per_group = abslistview.choice_mode_single; /** * 1 single choice groups */ public static final int choice_mode_single_absolute = 10001; private context context; public static arraylist<cadastraescolas> groups; private arraylist<arraylist<filhos>> child = new arraylist(); private hashmap<string, gpsescolas> amap = new hashmap<string, gpsescolas>(); private hashmap<string, gpsescolas> hashtask = new hashmap<string, gpsescolas>(); public static arraylist<filhos> listchild; private gridadapter adapter; private securepreferences sessao; public static customgridview gridview; private sparsearray<sparsebooleanarray> checkedpositions; private static final string log_tag = expandlistadapter.class.getsimplename(); public expandlisttest(context context, arraylist<cadastraescolas> groups, hashmap<string, gpsescolas> data, securepreferences msessao, hashmap<string, gpsescolas> hashtask) { this.context = context; this.groups = groups; this.amap = data; this.sessao = msessao; checkedpositions = new sparsearray<sparsebooleanarray>(); child = new arraylist(); if (groups != null) { (int = 0; < 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<filhos>(); (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 can contain number of views, views can have 1 parent viewgroup, if try add view has parent, exception (from viewgroup.addviewinner()):
throw new illegalstateexception("the specified child has parent. " + "you must call removeview() on child's parent first.");
this means structure of layouts of tree (not graph), , can use tree traversal algorithm iterate through every view of layout.
the following 1 of possible algorithms, post-order traversal.
it goes through root element, takes first child , makes recursive call algorithm, it's second, third etc...when there no children left go through, calls deselect function node.
for tree in diagram, nodes unselected in order:
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); }
you can find many other ways here: https://en.wikipedia.org/wiki/tree_traversal
it must learn how algorithms work if want ease programming experience.
Comments
Post a Comment