Forum Xamarin.Android

Problem with updating size of expandable list items

I'm currently implementing an expandable list view in my android application and I have some issues with updating it's size. The children in the list can change in size (since they in my case are expandable list themselves, resulting in a 3-level expandable list) but the added parts aren't visible since the original size of the list item doesn't change. There has to be a way to update the size every time a child item is clicked or something like that, but I can't seem to find it...

Could anyone give any suggestions on what methods to use or maybe another idea on how to solve this problem?

If you need more information please ask and I will try to answer as well as I can.

Posts

  • DWestyDWesty USMember ✭✭

    I had a small number of root nodes that I needed to do something similar. The way I solved it (right or wrong) was to take the root views out of a List view instead I put them in a scollview which contained a linear layout. Each of my root views was assigned a weight of 0 and height set to to wrap_content. As I added the children to the expandable views my root view would grow accordingly.

  • FZelleFZelle DEMember ✭✭✭

    Of course there is a way.

    1. GetChildrenCount(int groupPosition) is determined to get the number of childrows.
    2. Adapter.NotifyDataSetChanged updates all data and views in the expandableListAdapter.

    So update your data, call NotifyDataSetChanged of your adapter and it will repopulate your parentlist and the displayed childlist.

  • KurodaniKurodani SEMember

    @FZelle: That sounds like an awesome solution, but I'm not entirely sure how to implement it.

    Currently my primary expandable list adapter looks like this:

    public class IdeologyPrimaryListAdapter : BaseExpandableListAdapter
        {
            private Activity _context;
            private readonly IList<PrimGroup> _groups;
    
            public IdeologyPrimaryListAdapter(Activity context, IList<PrimGroup> groups)
            {
                _context = context;
                _groups = groups;
            }
    
            public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
            {
                return _groups[groupPosition].secGroup[childPosition];
            }
    
            public override long GetChildId(int groupPosition, int childPosition)
            {
                return (groupPosition * _groups.Count) + childPosition;
            }
    
            public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
            {
                ViewHolderExpandable vhe;
    
                View view = convertView;
                if (view == null) {
                    view = _context.LayoutInflater.Inflate(Resource.Layout.listItemExpandable, null);
    
                    vhe = new ViewHolderExpandable ();
    
                    vhe.Initialize (this, view);
                    view.Tag = vhe;
                }
                vhe = (ViewHolderExpandable)view.Tag;
                vhe.Bind (_context, _groups[groupPosition].secGroup);
                return view;
            }
    
            public override int GetChildrenCount(int groupPosition)
            {
                return _groups[groupPosition].secGroup.Count;
            }
    
            public override Java.Lang.Object GetGroup(int groupPosition)
            {
                return _groups[groupPosition];
            }
    
            public override long GetGroupId(int groupPosition)
            {
                return groupPosition;
            }
    
            public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
            {
                ViewHolder vh;
    
                View view = convertView;
                if (view == null) {
                    view = _context.LayoutInflater.Inflate(Resource.Layout.listItemLevel3, null);
    
                    vh = new ViewHolder ();
    
                    vh.Initialize (view);
                    view.Tag = vh;
                }
                vh = (ViewHolder)view.Tag;
                vh.Bind (_groups[groupPosition].name);
                return view;
            }
    
            public override bool IsChildSelectable(int groupPosition, int childPosition)
            {
                return true;
            }
    
            public override int GroupCount
            {
                get { return _groups.Count; }
            }
    
            public override bool HasStableIds
            {
                get { return true; }
            }
    
            private class ViewHolder : Java.Lang.Object
            {
                private TextView textViewTitle;
    
                public void Initialize(View view)
                {
                    textViewTitle = view.FindViewById<TextView> (Resource.Id.textViewListItemLvl3);
                }
    
                public void Bind (string titleStr)
                {
                    textViewTitle.Text = titleStr;
                }
            }
    
            private class ViewHolderExpandable : Java.Lang.Object
        {
            private ExpandableListView expandList;
    
            public void Initialize(IdeologyPrimaryListAdapter adapter, View view)
            {
                expandList = view.FindViewById<ExpandableListView> (Resource.Id.ideologyExpandableListView);
                expandList.GroupExpand += (sender, e) => RefreshListClick (sender, e, adapter);
                expandList.GroupCollapse += (sender, e) => RefreshListClick (sender, e, adapter);
            }
    
            public void Bind (Activity _context, IList<SecGroup> _group)
            {
                var secondAdapter = new IdeologySecondaryListAdapter (_context, _group);
                expandList.SetAdapter (secondAdapter);
            }
    
            private void RefreshListClick(object sender, EventArgs e, IdeologyPrimaryListAdapter adapter)
            {
                adapter.NotifyDataSetChanged ();
                Console.WriteLine ("Click");
            }
        }
        }
    

    Did you mean something like what I do in RefreshListClick? (the Console.WriteLine is just to confirm that the method is actually used) When I add the NotifyDataSetChanged part the secondary list doesn't seem to expand at all (at least according to the small arrow-icon on the left of the list item), but when I comment that method away the icon indicates that the next level has expanded but it's not shown. I'm pretty sure I've done some mistakes in my implementation, but not entirely sure where...

  • KurodaniKurodani SEMember

    I haven't been able to solve this problem yet, however I have found a new one... When the inner (secondary) expandable list inside the outer (primary) expandable list is filled with the list items the group position isn't updated for the secondary one in the GetGroupView method. The group position sent to the method is always 0 for some reason, even if the number of groups is correct.
    To clarify the problem, if I have a list of items that should be like this:

    PrimGroup1
      SecGroup1.1
      SecGroup1.2
      SecGroup1.3
    PrimGroup2
      SecGroup2.1
      SecGroup2.2
    

    it instead looks like this:

    PrimGroup1
      SecGroup1.1
      SecGroup1.1
      SecGroup1.1
    PrimGroup2
      SecGroup2.1
      SecGroup2.1
    

    I have no Idea why this is could anyone give me any suggestions? Also, should I keep this here since it's kind of related to my previous question or should I post this as a new thread?

Sign In or Register to comment.