2013-01-21 59 views
1

叫我有以下代碼:無法獲得listView.ItemClick在MonoDroid的

protected override void OnCreate (Bundle bundle) 
{ 
    progress = ProgressDialog.Show(this, "", "Loading..."); 
    progress.SetProgressStyle(ProgressDialogStyle.Spinner); 

    base.OnCreate (bundle); 

    //Set the Activity's view to our list layout   
    SetContentView(Resource.Layout.LawsAndRegsList); 

    new Thread(new ThreadStart(() => { 
     //Create our adapter 
     listAdapter = new LawsAndRegsListAdapter(this); 
     this.RunOnUiThread (() => { 
      //Find the listview reference 
      listView = FindViewById<ListView>(Resource.Id.listView); 

      //Hook up our adapter to our ListView 
      listView.Adapter = listAdapter; 

      //Wire up the click event 
      //listView.ItemClick += new EventHandler<ListView.ItemClickEventArgs>(listView_ItemClick); 
      listView.ItemClick += listView_ItemClick; 

      listAdapter.NotifyDataSetChanged(); 

      progress.Dismiss(); 
     }); 
    })).Start(); 
} 

void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) 
{ 
    //Get our item from the list adapter 
    var item = this.listAdapter.GetItemAtPosition(e.Position); 

    //Make a toast with the item name just to show it was clicked 
    Toast.MakeText(this, item.Name + " Clicked!", ToastLength.Short).Show(); 
} 

誰能告訴我,爲什麼會項目單擊不會被調用?

它看起來像我讀過的,我已經看到的例子,所以我做錯了什麼?謝謝您的幫助。

UPDATE:

這裏有兩個佈局我有:

LawsAndRegsList:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/widget28" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/topLevelMenuBackground" 
     android:orientation="horizontal"> 
     <Button 
      android:id="@+id/btnAddExpense" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:text="@string/ButtonHome" 
      android:layout_centerVertical="true" /> 
     <TextView 
      android:id="@+id/lblExpenseCancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="15dp" 
      android:text="@string/LawsAndRegs" 
      android:textColor="#ffffffff" 
      android:textSize="20sp" 
      android:layout_centerVertical="true" /> 
    </RelativeLayout> 
    <ListView 
     android:id="@+id/listView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

LawsAndRegsListItem:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/widget28" 
    android:layout_width="fill_parent" 
    android:background="@drawable/topLevelMenuCellBackground" 
    android:layout_height="40px"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <ImageView 
      android:id="@+id/imageItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center_vertical" /> 
     <TextView 
      android:id="@+id/textTop" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:textColor="#111479" 
      android:text="hello" 
      android:layout_toRightOf="@+id/imageItem" 
      android:textStyle="bold" 
      android:layout_marginLeft="10dp" /> 
     <ImageButton 
      android:src="@drawable/OrangeAccessoryButton" 
      android:layout_width="wrap_content" 
      android:layout_height="25dp" 
      android:layout_alignParentRight="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="15dp" 
      android:layout_centerVertical="true" 
      android:id="@+id/imageButton1" 
      android:background="@android:color/transparent" /> 
    </RelativeLayout> 
</LinearLayout> 

更新#2:

添加我的LIstAdapter代碼:

public class LawsAndRegsListAdapter:BaseAdapter { 活動上下文;

public List<LawsAndRegs> items; 

public LawsAndRegsListAdapter (Activity context) //We need a context to inflate our row view from 
{ 
    this.context = context; 

    MY_SEB_SERVICE_ContentWS.ContentWS contentWS = new MY_WEB_SERVICE_ContentWS.ContentWS(); 
    string sMenu = contentWS.GetMenuXML (146, 1, "", 0, 1033); 

    XmlRootAttribute r = new XmlRootAttribute ("Item"); 
    var ser = new XmlSerializer (typeof(Item), r); 
    StringReader stringReader; 
    stringReader = new StringReader (sMenu); 
    Item obj = (Item)ser.Deserialize (stringReader); 
    ItemMenuItem[] m = obj.Menu.Item; 
    ItemMenuItem larMenu = null; 
    foreach (ItemMenuItem mnu in m) 
    { 
     if (mnu.ItemID == 155) 
     { 
      if (this.items == null) 
       this.items = new List<LawsAndRegs>(); 
      larMenu = mnu; 
      break; 
     } 
    } 

    if (larMenu != null) 
    { 
     foreach (ItemMenuItemMenuItem imimi in larMenu.Menu.Item) 
     { 
      LawsAndRegs lar = new LawsAndRegs(); 
      lar.Name = (string)imimi.Items[2]; 
      lar.Image = (string)imimi.Items[3]; 
      this.items.Add (lar); 
     } 
    } 
} 

public override int Count 
{ 
    get { return items.Count; } 
} 

public override Java.Lang.Object GetItem(int position) 
{ 
    return position; 
} 

public override long GetItemId(int position) 
{ 
    return position; 
} 

public LawsAndRegs GetItemAtPosition(int position) 
{ 
    return items[position]; 
} 

public override View GetView(int position, View convertView, ViewGroup parent) 
{ 
    //Get our object for this position 
    var item = items[position];   

    //Try to reuse convertView if it's not null, otherwise inflate it from our item layout 
    // This gives us some performance gains by not always inflating a new view 
    // This will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell() 
    var view = (convertView ?? context.LayoutInflater.Inflate(Resource.Layout.LawsAndRegsListItem, parent, false)) as LinearLayout; 

    //Find references to each subview in the list item's view 
    ImageView imgView = (ImageView)view.FindViewById(Resource.Id.imageItem); 
    imgView.Clickable = true; 
    TextView textTop = (TextView)view.FindViewById(Resource.Id.textTop); 
    textTop.Clickable = true; 

    var bm = BitmapFactory.DecodeStream(
     new Java.Net.URL("http://www.SOME_SITE.com/" + item.Image).OpenStream()); 
    imgView.SetImageBitmap(bm); 

    //Assign this item's values to the various subviews 
    textTop.SetText(item.Name, TextView.BufferType.Normal); 

    view.Clickable = true; 

    //Finally return the view 
    return view; 
} 

}

我在試圖得到這個工作增加了view.clickable代碼。

+0

列表視圖中有哪些項目?你能告訴我們佈局的代碼嗎? –

+0

我更新了我的帖子並添加了構成我的列表的兩個佈局。 – LilMoke

回答

8

最後!我發現我的問題...原來,問題是我的ImageButton在ListView中。它正在竊取焦點並消耗觸摸事件。因此,在我的自定義適配器中,在GetView中,我需要執行以下操作:

var imageButton = view.FindViewById<ImageButton>(Resource.Id.imageButton1); 
imageButton.Focusable = false; 
imageButton.FocusableInTouchMode = false; 
imageButton.Clickable = true; 

imageButton.Click += (sender, args) => Console.WriteLine("ImageButton {0} clicked", position); 

從圖像按鈕中移除焦點。解決辦法在這裏給了我:Cheesebaron的http://forums.xamarin.com/discussion/871/how-do-i-get-itemclick-of-a-listview-to-get-called-not-sure-what-have-wrong#latest。謝謝Cheesebaron!

我希望這可以幫助另一個類似的問題新手!

+0

但你的問題是「無法得到listView.ItemClick被稱爲」,而不是圖像按鈕..很高興知道你已經解決了你的問題。 :D –

+1

是的,沒錯。 ItemClick沒有被調用,因爲該按鈕正在竊取焦點並消耗事件。如果我從佈局中刪除按鈕或放置上面的代碼ItemClickmgets調用。 – LilMoke

+0

如果您想防止圖像按鈕過度焦點(和事件),則需要將android:descendantFocusability =「blocksDescendants」添加到列表項的根LinearLayout中。這將阻止圖像按鈕竊取點擊。 –

6

android:descendantFocusability="blocksDescendants"放在您的LawsAndRegsListItem的線性佈局中,因爲Android不允許任何項目在listview中可以被聚焦。而不是將您的列表視圖中的每個控件設置爲android:focusable="false",最簡單的方法是將其根目錄ViewGroup設置爲android:descendantFocusability="blocksDescendants",因此它將阻止它的每個控件都變得更加專注。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/widget28" 
    android:layout_width="fill_parent" 
    android:background="@drawable/topLevelMenuCellBackground" 
    android:layout_height="40px" 
    android:descendantFocusability="blocksDescendants"> 
    ... 
</LinearLayout> 

此外,不要設置LinearLayoutClickabletrue。如果是這樣,即使它的孩子不會專注,並且因此listview將阻止任何點擊事件,它也會獲得焦點。

參考文獻:

ListView Tips & Tricks #4: Add several clickable areas

Android Documentation on descendantFocusability

+0

非常感謝您的幫助。我現在就試一試。然而,有沒有更好的方法,我應該做一個列表視圖?看起來像一個很常見的事情就是能夠在列表視圖中選擇一個單元格,也許我應該使用其他的東西? – LilMoke

+0

這很難說。這取決於設計。根據我的經驗,如果listview僅包含文字視圖或圖像,那麼您無法使用後代焦點。另一方面,如果你的設計需要有像按鈕,箱子等需要用戶交互的東西,我想你只需要處理它。 :) –

+0

事件仍然沒有被調用!我不明白,我不明白我有什麼錯。還有其他建議嗎? – LilMoke

0

我也設法通過在adapter :: GetView函數內設置「view.Click」處理程序來解決這個問題。似乎也工作。

public class TaskListAdapter : BaseAdapter<Task> { 
    Activity context = null; 
    IList<Task> tasks = new List<Task>(); 

    public TaskListAdapter (Activity context, IList<Task> tasks) : base() 
    { 
     this.context = context; 
     this.tasks = tasks; 
    } 

    [...] 

    public override Android.Views.View GetView (int position, Android.Views.View convertView, Android.Views.ViewGroup parent) 
    { 


     var view = (convertView ?? 
      context.LayoutInflater.Inflate(...)); 
     view.FindViewById<..>(Resource.Id.NameText).SetText ("...", TextView.BufferType.Normal); 

     view.Click += (sender, e) => 
     { 
      int a=0; 
      a++; //breakpoint location is being hit. 

     }; 
     return view; 
    } 
} 
}