get All Children View Of Type - Android User Interface

Android examples for User Interface:View Child

Description

get All Children View Of Type

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> getAllChildrenOfType(View view, Class classType) {
        List<View> listViews = getAllChildren(view);
        List<View> result = new ArrayList<>();
        for (View v : listViews) {
            if (classType.isInstance(v))
                result.add(v);/*www  . j  av a 2  s  .  com*/
        }

        return result;
    }

    public static List<View> getAllChildren(View view) {
        List<View> visited = new ArrayList<View>();
        List<View> unvisited = new ArrayList<View>();
        unvisited.add(view);

        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