java - How to generically retrieve all children of a certain type in a root layout? -


what mean question (if stated ambiguously, couldn't find answer question) take root layout, children of layout, , perform callback on instanceof specified type.

now, can in fixed way, doing like...

relativelayout root = (relativelayout) findviewbyid(r.id.root_layout);     for(int = 0; <= root.getchildcount(); i++){         view v = root.getchildat(i);         if(v instanceof customlayout){             // callback on view.         }     } 

thing is, want make more generic. should able use layout, , check see if instance of layout. in particular, want generic enough used (if possible). of course don't mind stopping @ settling layouts.

i want build collection of these children , return them, if possible of same type. haven't done java in long while i'm rusty, thinking of using reflection accomplish this. @ possible?

if pass class of type want, possible?

edit:

i didn't see dtann's answer before, must have missed it, did on own , looks similar his. implementation went along lines of this

public static abstract class callbackonrootchildren<t> {         @suppresswarnings("unchecked")         public void callonchildren(class<t> clazz, viewgroup root) {             for(int = 0; < root.getchildcount(); i++){                 view v = root.getchildat(i);                 if(v instanceof viewgroup){                     callonchildren(clazz, (viewgroup) v);                 }                 if(clazz.isassignablefrom(v.getclass())){                     // check see if assignable ensures it's type safe.                     onchild((t) v);                 }             }         }          public abstract void onchild(t child);     } 

difference mine relies on callbacks , whatnot, overall same concept.

try following code:

public <t> list<t>  getviewsbyclass(view rootview, class<t> targetclass) {     list<t> items = new arraylist<>();     getviewsbyclassrecursive(items,rootview,targetclass);     return items; }  private void getviewsbyclassrecursive(list items, view view, class clazz) {     if (view.getclass().equals(clazz)) {         log.d("tag","found " + view.getclass().getsimplename());         items.add(view);     }      if (view instanceof viewgroup) {         viewgroup viewgroup = (viewgroup)view;         if (viewgroup.getchildcount() > 0) {             (int = 0; < viewgroup.getchildcount(); i++) {                 getviewsbyclassrecursive(items, viewgroup.getchildat(i), clazz);             }         }     } } 

call getviewsbyclass , pass in root layout , target class. should receive list of views instance of target class. include root layout if instance of target class. method search entire view tree of root layout.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -