저도 같은 문제를 했어, 그것을 해결하기 위해 내가 ...의 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);
}
}
}
, 아마도 당신을 위해이 답변은 너무 늦었습니다. 그러나 나는 당신처럼 질문에 걸림돌이되는 것을 도울 수 있기를 바랍니다 ^^
나는 똑같은 문제를 겪었습니다. 찾았습니까? 이것에 대한 해결책? – Informatic0re