gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:20 25 4
gpt4 key购买 nike

所以我有一个可展开 View 的 gridview(用户点击它们,它们会动画展开)。问题是,当我单击第二列时,我的 gridview 只会增加行高。在第一张图片中,您可以看到我按下了列表中的第一项,它展开了,但是 gridview 没有更新它的行高。在第二张图片中,我单击了第二列中的第二个项目,它和 gridview 按预期展开。似乎 gridview 仅基于第二列进行测量。我怎样才能让它与第一列进行比较并选择较大的一列?谢谢。

编辑:这甚至发生在普通布局中。行宽始终是第二列的高度,即使该行中的第一列更高。

enter image description here

enter image description here

最佳答案

我遇到了同样的问题,为了解决这个问题,我不得不扩展 GridView 并使用一些肮脏的 hacks...

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

我知道,这个答案对你来说可能为时已晚,但我希望它能帮助像我一样遇到问题的人 ^^

关于Android GridView 行高是最后一列的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com