Forked from egslava/WrapContentHeightViewPager.java
Created
November 27, 2017 07:34
-
-
Save luongvo/f2caa4b7ae5daa4580b86bd1c3f128f1 to your computer and use it in GitHub Desktop.
Wrap content height ViewPager (Android)
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 org.cnii.layoutloader.ui; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* Special thanks to Daniel López Lacalle for his response | |
* (http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content/20784791#20784791) | |
* */ | |
public class WrapContentHeightViewPager extends ViewPager { | |
public WrapContentHeightViewPager(Context context) { | |
super(context); | |
} | |
public WrapContentHeightViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
protected 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