2015-10-20 82 views
0

我有一个可变高度单元格的GridView。我希望该行与行中所有最大的单元一样高。我可以调整单元格高度以便在一行上保持一致,但我无法设置GridView的高度并使其实际发生更改。Android设置GridView高度

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

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

GridView auto height calc wrong

我已经试过许多方法来更新它,但我相信,我失去了一些东西简单。我正在尝试在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似乎表明它是...

回答

0

我没太几个月前碰到过这个问题,所以有简单的解决方案。您需要继承自己的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); 
     } 
    } 

} 

希望它有帮助。