2017-08-03 14 views
1

我有一個Android應用程序,每當用戶點擊一個圖像(其中有幾個圖像)時,就會執行一個動作。 我想共同設立一個監聽這些圖像,讓我們說我們有6 與其說Xamarin Android:循環活動中的所有控件

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

我希望的一種方式來運行foreach循環設置點擊事件監聽器很快, 像

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

然後,我可以使用事件處理程序,使用的「發件人」參數,讓我來訪問被點擊的個人形象。

我已經嘗試閱讀Activity和Bundle類,但到目前爲止還沒有發現任何值得的東西。 這似乎不是一種常用的方法,因爲大多數搜索都剛剛爲「設置事件偵聽器」返回了一個解決方案。

回答

1

可以遍歷您的視圖層次,並得到所有你想要的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更高的內存效率的觀點。這對你來說可能是更好的解決方案。