Created
June 17, 2016 10:10
-
-
Save KevinOfNeu/8554caab1286e56cfc0dfe45acb6b43b to your computer and use it in GitHub Desktop.
MaxHeightListViews
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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