get All Children View - Android User Interface

Android examples for User Interface:View Child

Description

get All Children View

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<View> getAllChildren(View view) {
        List<View> visited = new ArrayList<View>();
        List<View> unvisited = new ArrayList<View>();
        unvisited.add(view);//from   w  w w .ja v  a  2 s  . co  m

        while (!unvisited.isEmpty()) {
            View child = unvisited.remove(0);
            visited.add(child);
            if (!(child instanceof ViewGroup))
                continue;
            ViewGroup group = (ViewGroup) child;
            final int childCount = group.getChildCount();
            for (int i = 0; i < childCount; i++)
                unvisited.add(group.getChildAt(i));
        }

        return visited;
    }
}

Related Tutorials