【问题标题】:Android Setting GridView HeightAndroid 设置 GridView 高度
【发布时间】:2016-01-18 14:18:28
【问题描述】:

我有一个带有可变高度单元格的 GridView。我希望该行与该行中所有最大的单元格一样高。我可以将单元格高度调整为在一行上保持一致,但我无法设置 GridView 的高度并让它实际改变。

另一个问题是这个GridView在一个ScrollView中,所以有滚动条是不可能的。

这是一个问题,因为 GridView 确定整个 Grid 高度的方式是取第一个单元格并乘以行数。如果行可以有不同的高度,这是一个明显的问题。例如:

我尝试了很多方法来更新它,但我确信我错过了一些简单的东西。我正在尝试在 ViewTreeObserver 中进行更新,所以我知道 GridView 已经渲染,所以我的计算是正确的(它们是正确的)。代码:

        ViewTreeObserver treeListener = mGridView.getViewTreeObserver(); 
        treeListener.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @SuppressLint("NewApi")
            @SuppressWarnings("deprecation")
            @Override 
            public void onGlobalLayout() { 
                if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } 
                else {
                    mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                // Calculate the new height we want for the GridView
                int newHeight = determineCellHeight(mGridView, mNumberOfColumns, mRows);

                ViewGroup.LayoutParams params = mGridView.getLayoutParams();
                params.height = newHeight;
                mGridView.setLayoutParams(params);

                // Have tried all of these too!!!
//              mAdapter.notifyDataSetChanged();
//              mGridView.requestLayout();
//              mGridView.invalidateViews();
//              mGridView.refreshDrawableState();

//              mGridView.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, newHeight + 10));
//              mGridView.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, newHeight + 10)); 
//                 View lastChild = mGridView.getChildAt( mGridView.getChildCount() - 1 );
//                 mGridView.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, lastChild.getBottom() ) );
//                  mAdapter.notifyDataSetChanged();
//                  mGridView.invalidateViews();
//                  mGridView.setMinimumHeight(newHeight);
//                  mGridView.requestLayout();
//                  mGridView.refreshDrawableState();

            } 
        });

我开始怀疑这是否可能,尽管众多 Stackflows 似乎表明它是...

【问题讨论】:

    标签: android gridview height


    【解决方案1】:

    几个月前我也遇到过这个问题,所以有一个简单的解决方案。您需要继承您自己的 GridView,并重写“onMeasure()”方法,以便计算您需要的实际高度。这是实现。

    public class ExpandableHeightGridView extends GridView {
    
        boolean expanded = false;
    
        public ExpandableHeightGridView(Context context) {
            super(context);
        }
    
        public ExpandableHeightGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ExpandableHeightGridView(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public boolean isExpanded() {
            return expanded;
        }
    
        public void setExpanded(boolean expanded) {
            this.expanded = expanded;
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            if (isExpanded()) {
                int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                        MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, expandSpec);
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2017-03-14
      • 2011-04-05
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多