可自适应条目高度的ViewPager

public class MyViewPager extends ViewPager{

public MyViewPager(Context context, AttributeSet attrs){

super(context, attrs);

}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

View view = getChildAt(getCurrentItem());

if (view != null) {

view.measure(widthMeasureSpec, heightMeasureSpec);

}

setMeasuredDimension(getMeasuredWidth(),

measureHeight(heightMeasureSpec, view));

}

private int measureHeight(int measureSpec, View view) {

int result = 0;

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {

result = specSize;

} else { 

if (view != null) {

result = view.getMeasuredHeight();

} if (specMode == MeasureSpec.AT_MOST) {

result = Math.min(result, specSize);

}

}

return result;

}

public void measeureView(View view)

{

int intw = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

int inth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

view view.measure(intw, inth);

// 获取测量后的view尺寸

int intwidth = view.getMeasuredWidth();

int intheight = view.getMeasuredHeight();

}

}

你可能感兴趣的:(可自适应条目高度的ViewPager)