Gets all views of a parent that match an instance (recursive) - Android User Interface

Android examples for User Interface:View Parent

Description

Gets all views of a parent that match an instance (recursive)

Demo Code


//package com.java2s;

import java.util.ArrayList;

import android.view.View;
import android.view.ViewGroup;

public class Main {
    /**/*from   ww  w  .j av  a  2  s.  co  m*/
     * Gets all views of a parent that match an instance (recursive)
     * @param parent The parent view
     * @param instance The instance to check
     * @return An array of views
     */
    public static ArrayList<View> getAllChildrenByInstance(
            ViewGroup parent, Class... instance) {
        ArrayList<View> views = new ArrayList<View>();
        int childCount = parent.getChildCount();

        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            View child = parent.getChildAt(childIndex);

            if (child instanceof ViewGroup) {
                views.addAll(getAllChildrenByInstance((ViewGroup) child,
                        instance));
            } else {
                for (Class c : instance) {
                    if (child.getClass() == c) {
                        views.add(child);
                    }
                }
            }
        }

        return views;
    }
}

Related Tutorials