title: Dynamic height viewpager
body: <p>I'm trying to create a custom viewpager inside custom scroll viewthat dynamically wraps the current child's height.</p>
  
  <pre><code>package com.example.vihaan.dynamicviewpager;
  
  import android.content.Context;
  import android.util.AttributeSet;
  import android.view.GestureDetector;
  import android.view.MotionEvent;
  import android.widget.ScrollView;
  
      /**
       * Created by vihaan on 1/9/15.
       */
      public class CustomScrollView extends ScrollView {
  
          private GestureDetector mGestureDetector;
  
          public CustomScrollView(Context context, AttributeSet attrs) {
              super(context, attrs);
              mGestureDetector = new GestureDetector(context, new YScrollDetector());
              setFadingEdgeLength(0);
          }
  
          @Override
          public boolean onInterceptTouchEvent(MotionEvent ev) {
              return super.onInterceptTouchEvent(ev)
                      &amp;&amp; mGestureDetector.onTouchEvent(ev);
          }
  
          // Return false if we're scrolling in the x direction
          class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
              @Override
              public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                      float distanceX, float distanceY) {
                  return (Math.abs(distanceY) &gt; Math.abs(distanceX));
              }
          }
      }
  </code></pre>
  
  <p>CustomPager</p>
  
  <pre><code>/**
   * Created by vihaan on 1/9/15.
   */
  public class CustomPager extends ViewPager {
  
      public CustomPager (Context context) {
          super(context);
      }
  
      public CustomPager (Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      @Override
      public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  
          boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
  
          final View tab = getChildAt(0);
          int width = getMeasuredWidth();
          int tabHeight = tab.getMeasuredHeight();
  
          if (wrapHeight) {
              // Keep the current measured width.
              widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
          }
  
          int fragmentHeight = measureFragment(((Fragment) getAdapter().instantiateItem(this, getCurrentItem())).getView());
          heightMeasureSpec = MeasureSpec.makeMeasureSpec(tabHeight + fragmentHeight + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), MeasureSpec.AT_MOST);
  
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }
  
      public int measureFragment(View view) {
          if (view == null)
              return 0;
  
          view.measure(0, 0);
          return view.getMeasuredHeight();
      }
  }
  </code></pre>
  
  <p>MyPagerAdapter</p>
  
  <pre><code>public class MyPagerAdapter extends FragmentPagerAdapter {
  
      private List&lt;Fragment&gt; fragments;
  
      public MyPagerAdapter(FragmentManager fm) {
          super(fm);
          this.fragments = new ArrayList&lt;Fragment&gt;();
          fragments.add(new FirstFragment());
          fragments.add(new SecondFragment());
          fragments.add(new ThirdFragment());
          fragments.add(new FourthFragment());
      }
  
      @Override
      public Fragment getItem(int position) {
          return fragments.get(position);
      }
  
      @Override
      public int getCount() {
          return fragments.size();
      }
  }
  </code></pre>
  
  <p>I was hoping that this would wrap around current fragments height but it is only taking the height of first child into consideration.</p>
  
  <p>Sample github project : <a href="https://github.com/VihaanVerma89/DynamicViewPager">https://github.com/VihaanVerma89/DynamicViewPager</a></p>