mercredi 30 septembre 2015

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

What I mean by my question (if I stated it ambiguously, as I couldn't find an answer to my question) is to take a root layout, get all children of that layout, and perform a callback on any that are an instanceof the specified type.

Now, I can do it in a fixed way, easily by doing something like...

RelativeLayout root = (RelativeLayout) findViewById(R.id.root_layout);
    for(int i = 0; i <= root.getChildCount(); i++){
        View v = root.getChildAt(i);
        if(v instanceof CustomLayout){
            // Do Callback on view.
        }
    }

Thing is, I want to make it more generic. I should be able to use any layout, and check to see if it is an instance of any layout. In particular, I want it generic enough to be used with anything (if this is even possible). Of course I don't mind stopping at just settling for layouts.

I want to build a collection of these children and return them, if possible of the same type. I haven't done Java in a long while so I'm very rusty, but I was thinking of using reflection to accomplish this. Is this at all possible?

If I pass the class of the type I want, is it possible?





Aucun commentaire:

Enregistrer un commentaire