listview auto height
public class NotListView extends ListView{
public NotListView(Context context) {
super(context);
}
public NotListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NotListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childHeight = getMeasuredHeight() - (getListPaddingTop() + getListPaddingBottom() + getVerticalFadingEdgeLength() * 2);
// on a first run let's have a space for at least one child so it'll trigger remeasurement
int fullHeight = getListPaddingTop() + getListPaddingBottom() + childHeight*(getCount());
int newChildHeight = 0;
for (int x = 0; x<getChildCount(); x++ ){
View childAt = getChildAt(x);
if (childAt != null) {
int height = childAt.getHeight();
newChildHeight += height;
}
}
//on a second run with actual items - use proper size
if (newChildHeight != 0)
fullHeight = getListPaddingTop() + getListPaddingBottom() + newChildHeight;
setMeasuredDimension(getMeasuredWidth(), fullHeight+30);
}
}