天天看点

ScrollView嵌套ListView,ListView高度自适应

之前做过一个项目,是ScrollView包含GridView和ListView,其中ListView和GridView无法自适应高度,研究了一下,需要重写onMeasure方法,ListView和GridView一样,废话不多说看源码:

import android.content.Context;

import android.util.AttributeSet;

import android.widget.ListView;

public class AdaptiveListView extends ListView {

public AdaptiveListView(Context context) {

super(context);

}

public AdaptiveListView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public AdaptiveListView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,

MeasureSpec.AT_MOST);

super.onMeasure(widthMeasureSpec, expandSpec);

}

}

转载于:https://www.cnblogs.com/androidDR/p/4545605.html