recursion - Android refer to custom views in a loop? -
i need recursively move through bunch of custom views of mine extend view class.
e.g.
viewone.java viewtwo.java viewthree.java
i have created instances of each view in mainclass.java
viewone vone; viewtwo vtwo; viewthree vthree;
these views implement function called start()
.
and want able loop through them somehow:
for(int i=0; i<= 2:i++) { views[i].start(); }
how go doing this?
the above example. real reason need able move through them numerically , programatically because want able add , remove views layout in numeric order button (previous , next) clicked. (i don't want them added layout @ start because heavily resource intensive views).
so required such:
click next -> add next view -> remove current view. click previous -> add previous view -> remove current view.
e.g.
currview = 1 current view currview (1) click next add view currview+1 (2) layout switch view currview+1 (2) remove view currview (1) or currview = 2 current view currview (2) click previous add view currview-1 (1) layout switch currview-1 (1) remove view currview (2)
note, views of own unique type , infact individual classes extend view. can't typecast them "view" because that's wrong, types viewone, viewtwo , viewthree respectively (for example).
assuming views have been added layout, can programatically iterate on children in viewgroup (i.e layout) so:
viewgroup group = findviewbyid(r.id.root); // name of layout int children = group.getchildcount(); (int = 0; < children; i++) { view child = group.getchildat(i); if (child instanceof viewone) { ... } else if (child instanceof viewtwo) { ... } }
additionally, if of custom views implement start()
, push method interface can simplify if
block above.
Comments
Post a Comment