ViewPager 高度自适应




public class ContentViewPager extends ViewPager {
public ContentViewPager(Context context) {
super(context);
}
public ContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int index = getCurrentItem();
int height = 0;
View v = ((Fragment) getAdapter().instantiateItem(this, index)).getView();
if (v != null) {
v.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
height = v.getMeasuredHeight();
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}