Unable to get listView.ItemClick to be called in MonoDroid

后端 未结 3 530
日久生厌
日久生厌 2020-12-20 06:07

I have the following code:

protected override void OnCreate (Bundle bundle)
{
    progress = ProgressDialog.Show(this, \"\", \"Loading...\");
    progress.Se         


        
相关标签:
3条回答
  • 2020-12-20 06:45

    Finally!! I found out my issue... it turned out that the problem was my ImageButton in the ListView. It was stealing the focus and consuming the touch events. So in my custom Adapter, in the GetView I needed to do something like the following:

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

    That removes the focus from the image button. The solution was given to me here: http://forums.xamarin.com/discussion/871/how-do-i-get-itemclick-of-a-listview-to-get-called-not-sure-what-have-wrong#latest by Cheesebaron. Thank you Cheesebaron!!!

    I hope this may help another newbie with a similar issue!!

    0 讨论(0)
  • 2020-12-20 06:53

    I also managed to get though this problem by setting the "view.Click" handler inside the adapter::GetView function. Seems to work as well.

    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;
        }
    }
    }
    
    0 讨论(0)
  • 2020-12-20 07:02

    Put android:descendantFocusability="blocksDescendants" in your linear layout of LawsAndRegsListItem, because Android doesn't allow any items to be focusable in listview. Instead of setting every control in your listview to android:focusable="false", the easiest way doing it is setting its root ViewGroup to android:descendantFocusability="blocksDescendants", so it will block every control of it getting focused.

    <?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>
    

    Also, don't set the LinearLayout's Clickable to true. If so, it will gain focused even though its child won't get focused and thus the listview will block any clicking event.

    References:

    ListView Tips & Tricks #4: Add several clickable areas

    Android Documentation on descendantFocusability

    0 讨论(0)
提交回复
热议问题