Xamarin Android:循环活动中的所有控件

Xamarin Android:循环活动中的所有控件

问题描述:

我有一个Android应用程序,每当用户点击一个图像(其中有几个图像)时,就会执行一个动作。 我想共同设立一个监听这些图像,让我们说我们有6 与其说Xamarin Android:循环活动中的所有控件

myImg0.Click += new MyImgClick; 
myImg1.Click += new MyImgClick; 

我希望的一种方式来运行foreach循环设置点击事件监听器很快, 像

foreach(img i in Application) 
{ 
    i.Click += new MyImgClick; 
} 

然后,我可以使用事件处理程序,使用的“发件人”参数,让我来访问被点击的个人形象。

我已经尝试阅读Activity和Bundle类,但到目前为止还没有发现任何值得的东西。 这似乎不是一种常用的方法,因为大多数搜索都刚刚为“设置事件侦听器”返回了一个解决方案。

可以遍历您的视图层次,并得到所有你想要的ImageViews用这样一个简单的递归方法:

private IEnumerable<T> GetViewsByType<T>(ViewGroup root) where T : View 
{ 
    var children = root.ChildCount; 
    var views = new List<T>(); 
    for (var i = 0; i < children; i++) 
    { 
     var child = root.GetChildAt(i); 
     if (child is T myChild) 
      views.Add(myChild); 
     else if (child is ViewGroup viewGroup) 
      views.AddRange(GetViewsByType<T>(viewGroup)); 
    } 
    return views; 
} 

任何ViewGroup类型的View将作为输入这里工作,所以LinearLayoutRelativeLayout

如果你不给你的根布局和ID你总是可以得到根用:

var root = Window.DecorView.FindViewById<ViewGroup>(Android.Resource.Id.Content); 

然后你可以运行在这根与方法,并得到所有ImageView S:

var imageViews = GetViewsByType<ImageView>(root); 

我已经在此布局测试这一点,它发现所有的ImageView很好,没有问题:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
     <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     </LinearLayout> 
    </LinearLayout> 
    </LinearLayout> 
    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    </RelativeLayout> 
    <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    </FrameLayout> 
</LinearLayout> 

无论是做这件事的最佳方式,我都非常怀疑它。也许你应该考虑一些关于使用RecyclerViewGridView或其他一些使用和Adapter更高的内存效率的观点。这对你来说可能是更好的解决方案。