1

我有以下代码:

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();
}

谁能告诉我为什么 ItemClick 不会被调用?

它看起来像我读过的和我看过的例子,所以我做错了什么?谢谢您的帮助。

更新:

这是我的两种布局:

法律和法规清单:

<?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 代码:

公共类 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 代码以尝试使其正常工作。

4

3 回答 3

10

最后!!我发现了我的问题......原来问题是我在 ListView 中的 ImageButton。它正在窃取焦点并消耗触摸事件。因此,在我的自定义适配器中,在 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);

这会从图像按钮中移除焦点。解决方案在这里给了我:http ://forums.xamarin.com/discussion/871/how-do-i-get-itemclick-of-a-listview-to-get-called-not-sure-what-错误的#latest by Cheesebaron。谢谢芝士男爵!!!

我希望这可以帮助另一个有类似问题的新手!

于 2013-01-22T17:09:49.827 回答
6

放入android:descendantFocusability="blocksDescendants"您的 LawsAndRegsListItem 线性布局,因为 Android 不允许任何项目在列表视图中成为焦点。与其将列表视图中的每个控件设置为android:focusable="false",最简单的方法是将其根设置ViewGroupandroid: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>

另外,不要将LinearLayout's设置Clickabletrue. 如果是这样,即使它的孩子不会获得焦点,它也会获得焦点,因此列表视图将阻止任何点击事件。

参考:

ListView 提示和技巧#4:添加几个可点击区域

关于descendantFocusability 的Android 文档

于 2013-01-21T19:26:44.187 回答
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;
    }
}
}
于 2014-05-22T15:25:48.770 回答