Android 遍历ViewGroup找出某种类型的所有子View

版权声明:转载请注明出处 https://blog.csdn.net/l707941510/article/details/82912526

要求不能用递归的方式实现

// 遍历viewGroup
public void traverseViewGroup(View view) {
	if(null == view) {
		return;
	}
	if(view instanceof ViewGroup) {
		ViewGroup viewGroup = (ViewGroup) view;
		LinkedList<ViewGroup> linkedList = new LinkedList<>();
		linkedList.add(viewGroup);
		while(!linkedList.isEmpty()) {
			ViewGroup current = linkedList.removeFirst();
			//dosomething
			for(int i = 0; i < current.getChildCount(); i ++) {
				if(current.getChildAt(i) instanceof ViewGroup) {
					linkedList.addLast((ViewGroup) current.getChildAt(i));
				}else {
					//dosomething
				}
			}
		}
	}else {
		//dosomething
	}
}

猜你喜欢

转载自blog.csdn.net/l707941510/article/details/82912526