get View from ViewGroup by path - Android User Interface

Android examples for User Interface:ViewGroup

Description

get View from ViewGroup by path

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;

public class Main {
    public static View getView(ViewGroup root, int[] path) {
        ViewGroup current = root;//from   w w w.j  a  v  a2s .  co  m
        for (int i = 0; i < path.length; i++) {
            if (current.getChildCount() <= path[i]) {
                return null;
            }
            View child = current.getChildAt(path[i]);
            if (i == path.length - 1) {
                return child;
            }
            if (!(child instanceof ViewGroup)) {
                return null;
            }
            current = (ViewGroup) child;
        }
        return null;
    }
}

Related Tutorials