Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在根布局中一般检索特定类型的所有子级?_Java_Android_Reflection_Android View_Android Relativelayout - Fatal编程技术网

Java 如何在根布局中一般检索特定类型的所有子级?

Java 如何在根布局中一般检索特定类型的所有子级?,java,android,reflection,android-view,android-relativelayout,Java,Android,Reflection,Android View,Android Relativelayout,我的问题(如果我含糊其辞,因为我找不到问题的答案)的意思是获取根布局,获取该布局的所有子级,并对任何指定类型的实例执行回调 现在,我可以用一种固定的方式来做,通过做一些像 RelativeLayout root = (RelativeLayout) findViewById(R.id.root_layout); for(int i = 0; i <= root.getChildCount(); i++){ View v = root.getChildAt(i);

我的问题(如果我含糊其辞,因为我找不到问题的答案)的意思是获取根布局,获取该布局的所有子级,并对任何指定类型的实例执行回调

现在,我可以用一种固定的方式来做,通过做一些像

RelativeLayout root = (RelativeLayout) findViewById(R.id.root_layout);
    for(int i = 0; i <= root.getChildCount(); i++){
        View v = root.getChildAt(i);
        if(v instanceof CustomLayout){
            // Do Callback on view.
        }
    }
RelativeLayout root=(RelativeLayout)findViewById(R.id.root\u布局);

对于(inti=0;i来说,没有通用的方法。如果有人这样做,他们也会这样做

viewgroup中的视图保存在字段中,如(源代码):


请尝试以下代码:

public <T> List<T>  getViewsByClass(View rootView, Class<T> targetClass) {
    List<T> items = new ArrayList<>();
    getViewsByClassRecursive(items,rootView,targetClass);
    return items;
}

private void getViewsByClassRecursive(List items, View view, Class clazz) {
    if (view.getClass().equals(clazz)) {
        Log.d("TAG","Found " + view.getClass().getSimpleName());
        items.add(view);
    }

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)view;
        if (viewGroup.getChildCount() > 0) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                getViewsByClassRecursive(items, viewGroup.getChildAt(i), clazz);
            }
        }
    }
}
公共列表getViewsByClass(视图根视图,类targetClass){
列表项=新建ArrayList();
getViewsByClassRecursive(项、根视图、目标类);
退货项目;
}
私有void getViewsByClassRecursive(列表项、视图、类clazz){
if(view.getClass().equals(clazz)){
Log.d(“TAG”,“Found”+view.getClass().getSimpleName());
添加(视图);
}
if(视图组的视图实例){
ViewGroup ViewGroup=(ViewGroup)视图;
如果(viewGroup.getChildCount()>0){
对于(int i=0;i

调用
getViewsByClass
并传入根布局和目标类。您应该会收到作为目标类实例的所有视图的列表。如果根布局本身也是目标类的实例,这将包括根布局本身。此方法将搜索根布局的整个视图树。

请参阅
class\isInstance(Object Object Object)
使用
Class#isInstance
而不是
Class#equals
,因此要匹配任何
视图
,您只需使用
View即可。Class
与我的解决方案非常相似,如果我早一点看到它,我就可以使用您的解决方案了。因此,孩子们都可以看到[]数组?简单地说,android没有检索所有视图的实现。您必须实现自己的方法,在视图层次结构中递归遍历,并对每个视图进行条件语句比较
// Child views of this ViewGroup
private View[] mChildren;
// Number of valid children in the mChildren array, the rest should be null or not
// considered as children
private int mChildrenCount;
public <T> List<T>  getViewsByClass(View rootView, Class<T> targetClass) {
    List<T> items = new ArrayList<>();
    getViewsByClassRecursive(items,rootView,targetClass);
    return items;
}

private void getViewsByClassRecursive(List items, View view, Class clazz) {
    if (view.getClass().equals(clazz)) {
        Log.d("TAG","Found " + view.getClass().getSimpleName());
        items.add(view);
    }

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)view;
        if (viewGroup.getChildCount() > 0) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                getViewsByClassRecursive(items, viewGroup.getChildAt(i), clazz);
            }
        }
    }
}