/
ViewGroupExtensions.cs
50 lines (43 loc) · 1.1 KB
/
ViewGroupExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections.Generic;
using AView = Android.Views.View;
using AViewGroup = Android.Views.ViewGroup;
namespace Microsoft.Maui.Platform
{
public static class ViewGroupExtensions
{
public static IEnumerable<T> GetChildrenOfType<T>(this AViewGroup viewGroup) where T : AView
{
for (var i = 0; i < viewGroup.ChildCount; i++)
{
AView? child = viewGroup.GetChildAt(i);
if (child is T typedChild)
yield return typedChild;
if (child is AViewGroup)
{
IEnumerable<T>? myChildren = (child as AViewGroup)?.GetChildrenOfType<T>();
if (myChildren != null)
foreach (T nextChild in myChildren)
yield return nextChild;
}
}
}
public static T? GetFirstChildOfType<T>(this AViewGroup viewGroup) where T : AView
{
for (var i = 0; i < viewGroup.ChildCount; i++)
{
AView? child = viewGroup.GetChildAt(i);
if (child is T typedChild)
return typedChild;
if (child is AViewGroup vg)
{
var descendant = vg.GetFirstChildOfType<T>();
if (descendant != null)
{
return descendant;
}
}
}
return null;
}
}
}