在ScrollView中嵌套使用了ListView/GridView,ListView/GridView只显示一行,为了解决这个问题,重写了ListView/GridView的onMeasure()方法。
通过改写onmesure方法可显示多行
public class WrapContentGridView extends GridView {
public WrapContentGridView (Context context) {
super(context);
}
public WrapContentGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
但回会导致adpter内 getview多次被调用,原因在于高度不定情况下,onmesure进行了多次测量,解决方法是标记view的测量时间,测量完成再进行数据的绑定。
public class WrapContentGridView extends GridView {
public boolean isOnMeasure ;
public WrapContentGridView (Context context) {
super(context);
}
public WrapContentGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
isOnMeasure = true;
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
isOnMeasure = false;
super.onLayout(changed, l, t, r, b);
}
}