Android的GridView行高与最后一列行高相同

4
我有一个可展开视图的GridView(用户点击它们并以动画方式展开)。问题是当我点击第二列时,我的GridView只增加了行高度。在第一张图片中,您可以看到我按下列表中的第一个项目,并且它扩展了,但GridView没有更新其行的高度。在第二张图片中,我选择了第二列中的第二个项目,并且它和GridView都按预期扩展了。似乎GridView只是基于第二列进行测量。如何使它与第一列进行比较并选择更大的那个?谢谢。
编辑: 即使使用简单的布局,这种情况也会发生。行宽始终是第二列的高度,即使该行中的第一列更高。

我遇到了完全相同的问题,你找到了解决方法吗? - Informatic0re
1个回答

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);
        }
    }
}

我知道,对于你来说可能太晚了,但是我希望它可以帮助像我一样遇到你的问题的人 ^^

这段话与 IT 技术无关,仅为一位网友给另一位留言的鼓励和祝愿。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接