Catch ExpandableListView group click event

Hi, I'm having serious difficulties catching click events on a ExpandableListView. It feels like I've been trying everything from adding Catch-event from ExpandableListView.GroupClick event to implmenting my own class that implements ExpandableListView.IOnGroupClickListener interface in combination with ExpandableListView.SetOnClickListener(). However, nothing seem to take effect. Have also been manipulating with the clickable and the focusable attributes on the Layout without success.

This is the first ExpandableListView I'm implementing so I'm not sure if I've done something really wrong! But my parent view is constituted by a standalone layout (.axml) with a linearLayout as root element, the same goes for the child-view.

Is there something wrong having the parent and child views be constituted by custom-layouts/views like this? The parent view is listed just fine, and also the child view (manged to see how it looked like by hardcoding a test using ExpandableListView.ExpandGroup()!).

The real problem why I want to Catch these events is that the ExpandableListView does **not ** expand when clicking on a Group item which I read somewhere that it should do by default, perhaps this give some hint on what might be wrong.

Answers

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    Can you share the project?

  • LarsNordlundLarsNordlund SEMember
    edited May 2013

    Absolutley, below are the releveant files (sorry for the bad-looking layout, tried make it code-style but rowbreaks did not work well)

    -----Main activity (Home/start screen) Note that the Catch click attempts have been tested one at a time and not all at once as in the code below :-) ----

    namespace MonoApplication
    {
    [Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]
    public class Home : Activity //, Android.Views.View.IOnClickListener
    {
    TextView textView;
    ListView errandsListView;
    List lstAllErrands;
    ProgressDialog waitDialog;
    ExpandableListView exList;
    GroupClickListener groupClick = new GroupClickListener();

    protected override void OnCreate (Bundle bundle)
    {
    base.OnCreate (bundle);
    SetContentView (Resource.Layout.Main);

    Button btnTest = (Button)FindViewById(Resource.Id.btnTest);
    btnTest.Click += new EventHandler(btnTest_Click);

    exList = (ExpandableListView)FindViewById(Resource.Id.expLWo);
    exList.SetAdapter(new WorkOrderExpListAdapter(this, (Context)this));
    exList.Clickable = true;
    exList.DividerHeight = 2;

    //Catch click event attempt 1
    exList.SetOnGroupClickListener(new OnGroupClickListener());

    //Catch click event attempt 2
    exList.GroupClick += (object pobjSender, ExpandableListView.GroupClickEventArgs pArgs) =>
    {
    test();
    };

    //Catch click event attempt 3
    exList.SetOnClickListener(new ClickListener());

    //Catch click event attempt 4
    exList.GroupClick += new EventHandler<ExpandableListView.GroupClickEventArgs>(exList_GroupClick);

    }

    void exList_GroupClick(object sender, ExpandableListView.GroupClickEventArgs e)
    {
    test();
    }

    void btnTest_Click(object sender, EventArgs e)
    {
    exList.ExpandGroup(1);
    }

    public void test()
    {
    throw new NotImplementedException();
    }
    }
    }

    The adapter for the exandablelistview (the idea is to populate the list by using a webservice Proxy but is hardcoded for now):

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Android.Widget;
    using MonoApplication.PlatformService;
    using Android.Content;
    using Android.App;
    using Android.Views;

    namespace MonoApplication.Adapters
    {
    public class WorkOrderExpListAdapter : BaseExpandableListAdapter
    {
    private Activity _activityContext;
    private Context _context;
    private IList lstWorkOrder = new List();
    private ServiceErrandWebService _proxy;

    public WorkOrderExpListAdapter(Activity activityContext, Context context)
    {
    this._context = context;
    this._activityContext = activityContext;
    // _proxy = ((ServiceErrandApp)_context.ApplicationContext).PlatformProxy;
    PopulateList();
    }

    private void PopulateList()
    {
    //return _proxy.GetErrands("WorkOrder", null, false, true);

    var wo1 = new WorkOrder()
    {
    Title = "Badkarsbyte",
    TaskDescription = "Badkar ska bytas och kopplas in.",
    Status = WorkOrderStatus.InProgress,
    Id = 1
    };

    var wo2 = new WorkOrder()
    {
    Title = "Packning duschblandare",
    TaskDescription = "Lagning av packning i dushblandare.",
    Status = WorkOrderStatus.InProgress,
    Id = 2
    };

    var wo3 = new WorkOrder()
    {
    Title = "Inkoppling proppskåp",
    TaskDescription = "Inkoppling av proppskåp i industrihall.",
    Status = WorkOrderStatus.InProgress,
    Id = 3
    };

    lstWorkOrder.Add(wo1);
    lstWorkOrder.Add(wo2);
    lstWorkOrder.Add(wo3);
    }

    public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
    {
    return MonoApplication.ObjectExtension.ToJavaObject(lstWorkOrder[groupPosition]);
    }

    public override long GetChildId(int groupPosition, int childPosition)
    {
    return lstWorkOrder[groupPosition].Id;
    }

    public override Android.Views.View GetChildView(int groupPosition, int childPosition, bool isLastChild, Android.Views.View convertView, Android.Views.ViewGroup parent)
    {
    View v = convertView;

    if (v == null)
    {
    v = _activityContext.LayoutInflater.Inflate(MonoApplication.Resource.Layout.WorkOrderListItemDetail, parent, false);
    }

    TextView woDescription = (TextView)v.FindViewById(Resource.Id.tvWoLiDDescription);
    TextView woStreet = (TextView)v.FindViewById(Resource.Id.tvWoLiStreet);
    TextView woZipCity = (TextView)v.FindViewById(Resource.Id.tvWoLiZipCity);
    TextView woDate = (TextView)v.FindViewById(Resource.Id.tvWoLiDDate);
    TextView woStatus = (TextView)v.FindViewById(Resource.Id.tvWoLiDStatus);

    var wo = lstWorkOrder[groupPosition];

    woDescription.Text = wo.TaskDescription;
    woStreet.Text = "Tallstigen 2";
    woZipCity.Text = "123 45, Stockholm";
    woDate.Text = "20130520";
    woStatus.Text = "Pågående";

    return v;
    }

    public override int GetChildrenCount(int groupPosition)
    {
    return 1;
    }

    public override Java.Lang.Object GetGroup(int groupPosition)
    {
    return MonoApplication.ObjectExtension.ToJavaObject(lstWorkOrder[groupPosition]);
    }

    public override long GetGroupId(int groupPosition)
    {
    return lstWorkOrder[groupPosition].Id;
    }

    public override Android.Views.View GetGroupView(int groupPosition, bool isExpanded, Android.Views.View convertView, Android.Views.ViewGroup parent)
    {
    View v = convertView;

    if (v == null)
    {
    v = _activityContext.LayoutInflater.Inflate(MonoApplication.Resource.Layout.WorkOrderListItem, parent, false);
    }

    TextView woTitle = (TextView)v.FindViewById(Resource.Id.tvWoLiTitle);
    TextView woPriority = (TextView)v.FindViewById(Resource.Id.tvWoLiPrio);
    ImageButton imgBtn = (ImageButton)v.FindViewById(Resource.Id.imgBtnWoList);

    v.SetOnClickListener(new MonoApplication.ViewClickLister());

    var wo = lstWorkOrder[groupPosition];

    woTitle.Text = wo.Title;
    woPriority.Text = "2";

    return v;
    }

    public override int GroupCount
    {
    get { return lstWorkOrder.Count; }
    }

    public override bool HasStableIds
    {
    get { return true; }
    }

    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
    return true;
    }
    }
    }

  • CheesebaronCheesebaron DKInsider, University mod

    Please read the Markdown documentation on how to format code on these forums (hint: indent it!)

  • LarsNordlundLarsNordlund SEMember

    Yeah did so, however thought the code got a bit messy without linebreaks that i did not manage to format. But here it is again!

    Main activity

    
    namespace MonoApplication
     {
     [Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]
     public class Home : Activity //, Android.Views.View.IOnClickListener
     {
     TextView textView;
     ListView errandsListView;
     List lstAllErrands;
     ProgressDialog waitDialog;
     ExpandableListView exList;
     GroupClickListener groupClick = new GroupClickListener();
    
    protected override void OnCreate (Bundle bundle) {
     base.OnCreate (bundle);
     SetContentView (Resource.Layout.Main);
    
    Button btnTest = (Button)FindViewById(Resource.Id.btnTest);
     btnTest.Click += new EventHandler(btnTest_Click);
    
    exList = (ExpandableListView)FindViewById(Resource.Id.expLWo);
     exList.SetAdapter(new WorkOrderExpListAdapter(this, (Context)this));
     exList.Clickable = true;
     exList.DividerHeight = 2;
    
    //Catch click event attempt 1
     exList.SetOnGroupClickListener(new OnGroupClickListener());
    
    //Catch click event attempt 2
     exList.GroupClick += (object pobjSender, ExpandableListView.GroupClickEventArgs pArgs) =>
     {
     test();
     };
    
    //Catch click event attempt 3
     exList.SetOnClickListener(new ClickListener());
    
    //Catch click event attempt 4
     exList.GroupClick += new EventHandler(exList_GroupClick);
    
    }
    
    void exList_GroupClick(object sender, ExpandableListView.GroupClickEventArgs e)
     {
     test();
     }
    
    void btnTest_Click(object sender, EventArgs e)
     {
     exList.ExpandGroup(1);
     }
    
    public void test()
     {
     throw new NotImplementedException();
     }
     } }
    

    And the adapter:

    
    using System;
     using System.Collections.Generic;
     using System.Text;
     using Android.Widget;
     using MonoApplication.PlatformService;
     using Android.Content;
     using Android.App;
     using Android.Views;
    
    namespace MonoApplication.Adapters
     {
     public class WorkOrderExpListAdapter : BaseExpandableListAdapter
     {
     private Activity _activityContext;
     private Context _context;
     private IList lstWorkOrder = new List();
     private ServiceErrandWebService _proxy;
    
    public WorkOrderExpListAdapter(Activity activityContext, Context context)
     {
     this._context = context;
     this._activityContext = activityContext;
     // _proxy = ((ServiceErrandApp)_context.ApplicationContext).PlatformProxy;
     PopulateList();
     }
    
    private void PopulateList()
     {
     //return _proxy.GetErrands("WorkOrder", null, false, true);
    
    var wo1 = new WorkOrder()
     {
     Title = "Badkarsbyte",
     TaskDescription = "Badkar ska bytas och kopplas in.",
     Status = WorkOrderStatus.InProgress,
     Id = 1
     };
    
    var wo2 = new WorkOrder()
     {
     Title = "Packning duschblandare",
     TaskDescription = "Lagning av packning i dushblandare.",
     Status = WorkOrderStatus.InProgress,
     Id = 2
     };
    
    var wo3 = new WorkOrder()
     {
     Title = "Inkoppling proppskåp",
     TaskDescription = "Inkoppling av proppskåp i industrihall.",
     Status = WorkOrderStatus.InProgress,
     Id = 3
     };
    
    lstWorkOrder.Add(wo1);
     lstWorkOrder.Add(wo2);
     lstWorkOrder.Add(wo3);
     }
    
    public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
     {
     return MonoApplication.ObjectExtension.ToJavaObject(lstWorkOrder[groupPosition]);
     }
    
    public override long GetChildId(int groupPosition, int childPosition)
     {
     return lstWorkOrder[groupPosition].Id;
     }
    
    public override Android.Views.View GetChildView(int groupPosition, int childPosition, bool isLastChild, Android.Views.View convertView, Android.Views.ViewGroup parent)
     {
     View v = convertView;
    
    if (v == null)
     {
     v = _activityContext.LayoutInflater.Inflate(MonoApplication.Resource.Layout.WorkOrderListItemDetail, parent, false);
     }
    
    TextView woDescription = (TextView)v.FindViewById(Resource.Id.tvWoLiDDescription);
     TextView woStreet = (TextView)v.FindViewById(Resource.Id.tvWoLiStreet);
     TextView woZipCity = (TextView)v.FindViewById(Resource.Id.tvWoLiZipCity);
     TextView woDate = (TextView)v.FindViewById(Resource.Id.tvWoLiDDate);
     TextView woStatus = (TextView)v.FindViewById(Resource.Id.tvWoLiDStatus);
    
    var wo = lstWorkOrder[groupPosition];
    
    woDescription.Text = wo.TaskDescription;
     woStreet.Text = "Tallstigen 2";
     woZipCity.Text = "123 45, Stockholm";
     woDate.Text = "20130520";
     woStatus.Text = "Pågående";
    
    return v;
     }
    
    public override int GetChildrenCount(int groupPosition)
     {
     return 1;
     }
    
    public override Java.Lang.Object GetGroup(int groupPosition)
     {
     return MonoApplication.ObjectExtension.ToJavaObject(lstWorkOrder[groupPosition]);
     }
    
    public override long GetGroupId(int groupPosition)
     {
     return lstWorkOrder[groupPosition].Id;
     }
    
    public override Android.Views.View GetGroupView(int groupPosition, bool isExpanded, Android.Views.View convertView, Android.Views.ViewGroup parent)
     {
     View v = convertView;
    
    if (v == null)
     {
     v = _activityContext.LayoutInflater.Inflate(MonoApplication.Resource.Layout.WorkOrderListItem, parent, false);
     }
    
    TextView woTitle = (TextView)v.FindViewById(Resource.Id.tvWoLiTitle);
     TextView woPriority = (TextView)v.FindViewById(Resource.Id.tvWoLiPrio);
     ImageButton imgBtn = (ImageButton)v.FindViewById(Resource.Id.imgBtnWoList);
    
    v.SetOnClickListener(new MonoApplication.ViewClickLister());
    
    var wo = lstWorkOrder[groupPosition];
    
    woTitle.Text = wo.Title;
     woPriority.Text = "2";
    
    return v;
     }
    
    public override int GroupCount
     {
     get { return lstWorkOrder.Count; }
     }
    
    public override bool HasStableIds
     {
     get { return true; }
     }
    
    public override bool IsChildSelectable(int groupPosition, int childPosition)
     {
     return true;
     }
     }
     }
    

    Thanks for any help with this!

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    Is there any way you can share the project itself, as an attached zip file?

  • GautamJainGautamJain INMember ✭✭✭
    edited March 2017

    To just get the child and group click events, add the following code in your activity OnCreate:

    listView.ChildClick += ListView_ChildClick;
    listView.GroupClick += ListView_GroupClick;
    

    And add following handlers:

    private void ListView_GroupClick(object sender, ExpandableListView.GroupClickEventArgs e)
    {
    
    }
    
    private void ListView_ChildClick(object sender, ExpandableListView.ChildClickEventArgs e)
    {
    
    }
    
  • SumeetKumar.6844SumeetKumar.6844 USMember
    edited October 2017

    @Gautam_Jain

    Sir, how do we know that which child item has been clicked. And also How should a new activity page get opened on child item click?

Sign In or Register to comment.