Pinned groups in ExpandableListView

前端 未结 5 512
Happy的楠姐
Happy的楠姐 2020-12-24 04:10

Is there a standard way to pin group item to the top of screen while scrolling group\'s items. I saw similar examples with ListView. What interfaces should I implement or wh

5条回答
  •  一个人的身影
    2020-12-24 04:23

    I have a really simple solution using AXML and C#. The group will never move and children will be expandable and scrollable.

    I'll show you here a case where there is only one group in the list, with the closed state at start.

    This solution even works inside scrollviews, viewpagers etc...

    1. In your view, hard code your group view at the top of your ExpandableListView, with LinearLayout or whatever you need (this will never move ;-) ) and set the visibility property of your ExpandableListView to Gone.

      
          
          
      
      
      
    2. Keep using your Adapter, but we have to hide the group view cause we hard coded it in the first step. Just set a LinearLayout with height property to 0dp

    Adapter

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {            
            var inflater = _context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
            convertView = inflater.Inflate(Resource.Layout.group_visibility_gone, null);
    
            return convertView;
        }
    

    AXML template

        
        
    
    1. The event that expand the list is fired by the group we just came to hide. So let's keep it always open : expandableListViewNotes.ExpandGroup(0);

    2. Let's create an event by ourslef on the hard coded group

      private void llExpandableNotes_Clicked(object sender, EventArgs e)
      {
          switch(expandableListViewNotes.Visibility)
          {
              case ViewStates.Gone:
                  imgExpandable.SetImageResource(Resource.Drawable.icon_minus_red);
                  txtExapndableNotes.SetTextColor(Android.Graphics.Color.Red);
                  expandableListViewNotes.Visibility = ViewStates.Visible;
                  break;
      
              case ViewStates.Visible:
                  imgExpandable.SetImageResource(Resource.Drawable.icon_plus_black);
                  txtExapndableNotes.SetTextColor(Android.Graphics.Color.Black);
                  expandableListViewNotes.Visibility = ViewStates.Gone;
                  break;
          }
      }
      

    SUMMARY :

    • We have an ExpandableListView with an invisible group view (height=0dp)
    • We keep it always opened with ExpandGroup method
    • We create a fake header at the top of the ExapandableListView
    • ExpandableListView Visibility property starts with Gone state
    • We create a Click event on the fake group that set visibility on expandableListView to Visible or Gone.

    And that's all ! Enjoy !

提交回复
热议问题