2015-10-20 52 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); 
     } 
    } 

} 

希望它有幫助。