Gridview height gets cut

后端 未结 6 1533
刺人心
刺人心 2020-11-22 17:26

I\'m trying to display 8 items inside a gridview. Sadly, the gridview height is always too little, so that it only shows the first row, and a little part of the second.

6条回答
  •  孤独总比滥情好
    2020-11-22 17:54

    Found tacones answer helpfull... so i ported it to C# (Xamarin)

    public class ExpandableHeightGridView: GridView
    {
        bool _isExpanded = false;
    
        public ExpandableHeightGridView(Context context) : base(context)
        {            
        }
    
        public ExpandableHeightGridView(Context context, IAttributeSet attrs) : base(context, attrs)
        {            
        }
    
        public ExpandableHeightGridView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
        {            
        }
    
        public bool IsExpanded
        {
            get { return _isExpanded; }
    
            set { _isExpanded = value;  }
        }
    
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            // HACK! TAKE THAT ANDROID!
            if (IsExpanded)
            {
                // Calculate entire height by providing a very large height hint.
                // View.MEASURED_SIZE_MASK represents the largest height possible.
                int expandSpec = MeasureSpec.MakeMeasureSpec( View.MeasuredSizeMask, MeasureSpecMode.AtMost);
                base.OnMeasure(widthMeasureSpec,expandSpec);                
    
                var layoutParameters = this.LayoutParameters;
                layoutParameters.Height = this.MeasuredHeight;
            }
            else
            {
                base.OnMeasure(widthMeasureSpec,heightMeasureSpec);    
            }
        }
    }
    

提交回复
热议问题