get descended views from parent. - Android User Interface

Android examples for User Interface:View Parent

Description

get descended views from parent.

Demo Code


//package com.java2s;

import java.util.ArrayList;
import java.util.List;
import android.view.View;

import android.view.ViewGroup;

public class Main {

    public static <T extends View> List<T> getDescendants(ViewGroup parent,
            Class<T> filter, boolean includeSubClass) {

        List<T> descendedViewList = new ArrayList<T>();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            Class<? extends View> childsClass = child.getClass();
            if ((includeSubClass && filter.isAssignableFrom(childsClass))
                    || (!includeSubClass && childsClass == filter)) {
                descendedViewList.add(filter.cast(child));
            }/*  w w w .jav  a2s.co m*/
            if (child instanceof ViewGroup) {
                descendedViewList.addAll(getDescendants((ViewGroup) child,
                        filter, includeSubClass));
            }
        }
        return descendedViewList;
    }
}

Related Tutorials