Last active
October 7, 2016 23:40
-
-
Save Bramengton/ed3e6ea44e8739c54019d4890ab17ad7 to your computer and use it in GitHub Desktop.
Setting wrap_content on a ViewPager's layout_height in XML doesn't seem to be recognized and the ViewPager will fill the height of the screen regardless. We'll force the ViewPager to have the same height as its immediate child.
This file contains 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
package com.sample.daterange; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* @author Bramengton | |
* @date on 07/10/2016. | |
* @change on 07/10/2016. | |
*/ | |
public class CustomViewPager extends ViewPager { | |
public CustomViewPager(Context context){ | |
super(context); | |
} | |
public CustomViewPager(Context context, AttributeSet attrs){ | |
super(context, attrs); | |
} | |
/** | |
* Setting wrap_content on a ViewPager's layout_height in XML | |
* doesn't seem to be recognized and the ViewPager will fill the | |
* height of the screen regardless. We'll force the ViewPager to | |
* have the same height as its immediate child. | |
*/ | |
@Override | |
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ | |
int height = 0; | |
for (int i = 0; i < getChildCount(); i++){ | |
View child = getChildAt(i); | |
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); | |
int h = child.getMeasuredHeight(); | |
if (h > height) height = h; | |
} | |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment