Search code examples
androidandroid-listviewexpandablelistviewandroid-custom-viewonitemclicklistener

monodroid expandablelistview.itemclick does not get called with customview c#


I have been trying days to get this work and found many solutions, but none worked. The click event is not fired in the listview. Also the touch event of the activity does not get fired. I have added android:descendantFocusability="blocksDescendants" to the layout, but this does not help. Here is my Code:

Main Activity:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        RequestWindowFeature(WindowFeatures.Progress);
        RequestWindowFeature(WindowFeatures.IndeterminateProgress);
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        lv = FindViewById<ExpandableListView> (Resource.Id.lvItems);
        FooterView = new TextView (this);
        lv.AddFooterView (FooterView);
        FooterView.Text = "Fling Velocity: ";
        lv.ChoiceMode = ChoiceMode.Multiple;
        lv.Clickable = true;
        lv.Focusable = true;
        lv.FocusableInTouchMode = true;
        //lv.Touch += (object sender, View.TouchEventArgs e) => {this.OnTouchEvent(e.Event); e.Handled = false;};

        //_gestureDetector = new GestureDetector(this);
        //_ScaleGestureDetector = new ScaleGestureDetector (this,this);
        System.Collections.Generic.List<ImgFolder> BMList = new System.Collections.Generic.List<ImgFolder>();
        System.Diagnostics.Debug.Print (Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).Path);

        string selection = null;
        string[] selectionArgs = null;
        string[] projection = new string[] {MediaStore.MediaColumns.Id, MediaStore.MediaColumns.Data,
            MediaStore.Images.Media.InterfaceConsts.BucketDisplayName, MediaStore.Images.Media.InterfaceConsts.BucketId
        }; 

        Android.Database.ICursor mediaCursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri,
            projection, selection,selectionArgs, null);
        lib.GetThumbnails (this,false,mediaCursor, BMList);

        mediaCursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.InternalContentUri,projection,selection,selectionArgs, null);
        lib.GetThumbnails (this,true,mediaCursor, BMList);

            //ArrayAdapter Adapter = new ArrayAdapter<ImgListItem>(this,Android.Resource.Layout.ActivityListItem,BMList);
        PhotoFolderAdapter Adapter = new PhotoFolderAdapter (this, BMList);
        lv.SetAdapter (Adapter);
        lv.ItemClick += lv_ItemClick;
        /* (object sender, AdapterView.ItemClickEventArgs e) => {
            View v = e.View;
            if (v.Tag != null) {
                ViewHolder holder = (ViewHolder)(v.Tag);
                ImgListItem ImgListItem = holder.item;
                lib.StartViewer (this, ImgListItem.Uri);
            }
        };
        */
        ///new EventHandler<AdapterView.ItemClickEventArgs>(lv_ItemClick);

    }
    private void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        View v = e.View;
        if (v.Tag != null) {
            ViewHolder holder = (ViewHolder)(v.Tag);
            ImgListItem ImgListItem = holder.item;
            lib.StartViewer (this, ImgListItem.Uri);
            }

    }

Adapter:

     public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        // Recycle a previous view if provided:
        View view = convertView;

        // If no recycled view, inflate a new view as a simple expandable list item 2:
        if (view == null)
        {
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView,null);
        }
        ImgListItem item = rows [groupPosition].items[childPosition];
        ImageView Image = view.FindViewById<ImageView> (Resource.Id.Image); 
        Image.SetImageBitmap (item.Img);
        Image.Clickable = false;
        Image.Focusable = false;
        Image.FocusableInTouchMode = false;
        Image.LongClickable = false;
        //Image.Click+= (object sender, EventArgs e) => Console.WriteLine("IMG clicked");
        //view.FindViewById<ImageView> (Android.Resource.Id.Icon).ScaleX = 5;
        //view.FindViewById<ImageView> (Android.Resource.Id.Icon).ScaleY = 5;
        TextView Text1 = view.FindViewById<TextView> (Resource.Id.Text1); 
        Text1.Text = item.FileName;
        Text1.Clickable = false;
        Text1.Focusable = false;
        Text1.FocusableInTouchMode = false;
        Text1.LongClickable = false;
        //Text1.Click+= (object sender, EventArgs e) => Console.WriteLine("Text1 clicked");
        if (item.Img != null) {
            TextView Text2 = view.FindViewById<TextView> (Resource.Id.Text2); 
            Text2.Text = (item.size != null) ? item.size : item.Img.Width + "*" + item.Img.Height;
            Text2.Clickable = false;
            Text2.Focusable = false;
            Text2.FocusableInTouchMode = false;
            Text2.LongClickable = false;
            //Text2.Click += (object sender, EventArgs e) => Console.WriteLine("Text2 clicked");
        }
        //if (item.Img != null) view.FindViewById<TextView> (Resource.Id.Text2).Text = (item.size != null) ? item.size : item.Img.Width + "*" + item.Img.Height;
        //view.FindViewById<TextView> (Android.Resource.Id.Text1).
        view.Tag = new ViewHolder(item);
        //view.ScaleX = 2;
        //view.ScaleY = 2;
        view.Clickable = false;
        view.Focusable = false;
        view.FocusableInTouchMode = false;
        view.LongClickable = false;
        /*view.Click += (object sender, EventArgs e) => {
            System.Diagnostics.Debug.Print ("Click");
        };
        */
        //view.Touch += new EventHandler<View.TouchEvent1Args> (touch);
        return view;
    }

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFDAFF7F"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:padding="1dp"
    android:id="@+id/ImageList">
    <ImageView
        android:id="@+id/Image"
        android:layout_height="78dp"
        android:layout_width="78dp"
        android:padding="1dp"
        android:src="@drawable/res"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:layout_alignParentLeft="true"
        android:longClickable="false" />
    <LinearLayout
        android:id="@+id/Text"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:focusable="false"
        android:layout_marginLeft="80dp">
        <TextView
            android:id="@+id/Text1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".70"
            android:textColor="#FF7F3300"
            android:textSize="20dp"
            android:textStyle="italic"
            android:text="Testing 1234"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:longClickable="false"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="20dp" />
        <TextView
            android:id="@+id/Text2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".25"
            android:textSize="14dp"
            android:layout_marginTop="23dp"
            android:textColor="#FF267F00"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:text="Testing 1234"
            android:longClickable="false"
            android:layout_gravity="center_vertical" />
    </LinearLayout>
</RelativeLayout>

Solution

  • It seems to me, that the ItemClick Event is not implemented in monodroid. Using the ChildClick Event instead works.