简体   繁体   English

Android GridView行高是最后一列的行高

[英]Android GridView row height is last column's row's height

So I have a gridview of expandable views (user taps on them and they animate to expand). 因此,我有一个可扩展视图的gridview(用户轻按它们,它们就会动画以进行扩展)。 The thing is that my gridview only grows the row height when I click on the second column. 问题是,当我单击第二列时,gridview只会增加行高。 In the first picture, you can see I pressed the first item in the list and it expanded but the gridview did not update its rows' heights. 在第一张图片中,您可以看到我按了列表中的第一项并将其展开,但是gridview并未更新其行的高度。 In the second picture, I clicked on the second item in the second column and it and the gridview expanded as intended. 在第二张图片中,我单击了第二列中的第二项,并且网格视图按预期扩展。 It seems the gridview is only measuring based on the second column. 看来gridview仅基于第二列进行测量。 How can I make it compare it to the first column and pick the larger one? 如何使其与第一列进行比较并选择较大的列? thanks. 谢谢。

EDIT: This is even happening with plain layouts. 编辑:这甚至发生在普通布局中。 The row width is always the second column's height, even if the first column in that row is taller. 行宽度始终是第二列的高度,即使该行中的第一列较高。

在此处输入图片说明

在此处输入图片说明

I've had the same problem, to fix it I had to extend GridView and use a couple of dirty hacks... 我遇到了同样的问题,要解决此问题,我不得不扩展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);
        }
    }
}

I know, probably for you this answer is too late, but I hope it can help whoever is stumbling upon your question like I did ^^ 我知道,可能对您而言,这个答案为时已晚,但我希望它能像我一样对遇到您的问题绊绊的人提供帮助^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM