2013-08-09 1 views
3

그래서 확장 가능한 뷰 (사용자 탭과 펼치기 애니메이션)가있는 gridview가 있습니다. 것은 두 번째 열을 클릭 할 때 내 gridview 경우에만 행 높이가 커집니다. 첫 번째 그림에서 목록의 첫 번째 항목을 눌렀는데 확대되었지만 gridview가 행의 높이를 업데이트하지 않았다는 것을 알 수 있습니다. 두 번째 그림에서는 두 번째 열의 두 번째 항목을 클릭하고 그리드 뷰를 의도대로 확장했습니다. 그것은 gridview는 두 번째 열을 기반으로 측정하는 것 같습니다. 첫 번째 열과 비교하고 큰 열을 선택하려면 어떻게해야합니까? 감사.Android GridView 행 높이가 마지막 열의 행 높이입니다.

편집 : 이것은 일반 레이아웃에서도 발생합니다. 행 너비는 해당 행의 첫 번째 열이 더 높더라도 항상 두 번째 열의 높이입니다.

enter image description here

enter image description here

+0

나는 똑같은 문제를 겪었습니다. 찾았습니까? 이것에 대한 해결책? – Informatic0re

답변

2

저도 같은 문제를 했어, 그것을 해결하기 위해 내가 ...의 GridView를 확장하고 더러운 해킹의 몇 가지를 사용했다

내가 아는
public class AutoGridView extends GridView { 
    public AutoGridView(Context context, AttributeSet attrs, int defStyle){ 
     super(context, attrs, defStyle); 
    } 

    public AutoGridView(Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    public AutoGridView(Context context){ 
     super(context); 
    } 

    @Override 
    protected void layoutChildren(){ 
    /*This method is called during onLayout. I let the superclass make the hard 
    part, then I change the top and bottom of visible items according to their 
    actual height and that of their siblings*/ 

     final int childrenCount = getChildCount(); 
     if(childrenCount <= 0){ //nothing to do, just call the super implementation 
      super.layoutChildren(); 
      return; 
     } 

     /*GridView uses the bottom of the last child to decide if and how to scroll. 
     UGLY HACK: ensure the last child bottom is equal to the lowest one. 
     Since super implementation might invalidate layout I must be sure the old 
     value is restored after the call*/ 
     View v = getChildAt(childrenCount - 1); 
     int oldBottom = v.getBottom(); 
     super.layoutChildren(); 
     v.setBottom(oldBottom); 

     for(int i = 0; i < getNumColumns(); ++i){ 
      int top = getListPaddingTop(); 
      for(int j = i; j < childrenCount; j += getNumColumns()){ 
       v = getChildAt(j); 
       /*after setTop v.getHeight() returns a different value, 
       that's why I'm saving it...*/ 
       int viewHeight = v.getHeight(); 
       v.setTop(top); 
       top += viewHeight; 
       v.setBottom(top); 
       top += getVerticalSpacing(); 
      } 
     } 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     final int childrenCount = getChildCount(); 
     if(getAdapter() != null && childrenCount > 0){ 
      measureChildren(widthMeasureSpec, heightMeasureSpec); 
      int width = getSize(widthMeasureSpec); 
      int heightSize = getSize(heightMeasureSpec); 
      int heightMode = getMode(heightMeasureSpec); 

      int maxHeight = heightSize; 
      for(int i = 0; i < getNumColumns(); ++i){ 
       int columnHeight = 0; 
       for(int j = i; j < childrenCount; j += getNumColumns()) 
        columnHeight += getChildAt(j).getMeasuredHeight() + getVerticalSpacing(); 
       maxHeight = Math.max(maxHeight, columnHeight); 
      } 

      getChildAt(childrenCount-1).setBottom(maxHeight); // for the scrolling hack 

      int height = heightSize; 
      if(heightMode == UNSPECIFIED) 
       height = maxHeight; 
      else if(heightMode == AT_MOST) 
       height = Math.min(maxHeight, heightSize); 

      setMeasuredDimension(width, height); 
     } 
    } 
} 

, 아마도 당신을 위해이 답변은 너무 늦었습니다. 그러나 나는 당신처럼 질문에 걸림돌이되는 것을 도울 수 있기를 바랍니다 ^^