Skip to content

Instantly share code, notes, and snippets.

@KevinOfNeu
Created June 17, 2016 10:10
Show Gist options
  • Select an option

  • Save KevinOfNeu/8554caab1286e56cfc0dfe45acb6b43b to your computer and use it in GitHub Desktop.

Select an option

Save KevinOfNeu/8554caab1286e56cfc0dfe45acb6b43b to your computer and use it in GitHub Desktop.
MaxHeightListViews
public class MaxHeightListViews extends ListView {
int maxHeight;
public MaxHeightListViews(Context context) {
super(context);
setViewHeight();
}
public MaxHeightListViews(Context context, AttributeSet attrs) {
super(context, attrs);
setViewHeight();
}
public MaxHeightListViews(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setViewHeight();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (maxHeight > 0) {
int hSize = MeasureSpec.getSize(heightMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
switch (hMode) {
case MeasureSpec.AT_MOST:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST);
break;
case MeasureSpec.UNSPECIFIED:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
break;
case MeasureSpec.EXACTLY:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY);
break;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void setViewHeight() {
maxHeight = (int) getContext().getResources().getDimension(R.dimen.order_dialog_fragment_height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment