美文网首页
可自适应条目高度的ViewPager

可自适应条目高度的ViewPager

作者: 举个小栗子0317 | 来源:发表于2018-12-10 18:40 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/otpdhqtx.html