public static IEnumerable <View> EnumerateAllChildren(this ViewGroup view, int maxDepth = 20) { foreach (var sub in view.GetChildren()) { yield return(sub); if (maxDepth > 0) { if (sub is ViewGroup childGroup) { foreach (var subResult in childGroup.EnumerateAllChildren(maxDepth - 1)) { yield return(subResult); } } } } }
/// <summary> /// Enumerates all the children for a specified view group. /// </summary> /// <param name="view">The view group to get the children from</param> /// <param name="maxDepth">The depth to stop looking for children.</param> /// <returns>A lazy enumerable of views</returns> public static IEnumerable<View> EnumerateAllChildren(this ViewGroup view, int maxDepth = 20) { foreach (var sub in view.GetChildren()) { yield return sub; if (maxDepth > 0) { var childGroup = sub as ViewGroup; if (childGroup != null) { foreach (var subResult in childGroup.EnumerateAllChildren(maxDepth - 1)) { yield return subResult; } } } } }
/// <summary> /// Enumerates all the children for a specified view group. /// </summary> /// <param name="view">The view group to get the children from</param> /// <param name="selector">The selector function</param> /// <param name="maxDepth">The depth to stop looking for children.</param> /// <returns>A lazy enumerable of views</returns> public static IEnumerable <View> EnumerateAllChildren(this ViewGroup view, Func <View, bool> selector, int maxDepth = 20) { foreach (var sub in view.GetChildren()) { if (selector(sub)) { yield return(sub); } else if (maxDepth > 0) { var childGroup = sub as ViewGroup; if (childGroup != null) { foreach (var subResult in childGroup.EnumerateAllChildren(selector, maxDepth - 1)) { yield return(subResult); } } } } }
public static IEnumerable <T> GetChildren <T>(this ViewGroup view) => view.GetChildren().OfType <T>();