title: Setting viewpager height to content's height
body: <p>I have an expandablelistview which contains multiple viewpagers, and each viewpager contains multiple pages which each have a linearlayout of data. The data in the linearlayout is dynamic (there could be 5 rows or there could be 20), and each row has a dynamic height (it could have 1 line of text or 2-3). </p>
  
  <p>As the viewpager requires a hard-coded height to be set for it to display, I need to display all of the content in each page's linearlayout, and I can't allow the content within the viewpager to scroll, I'm in the difficult situation of trying to calculate the height of the viewpager's active page view and then dynamically update the height of the viewpager so that the whole linearlayout content is visible. As a new page is swiped to, it should then update the viewpager height accordingly.</p>
  
  <p>I'm attempting to do this within the pager adapter's SetPrimaryItem method, however the height that is coming back is close but not accurate. In my pager page view I have an outer relativelayout with a blue background, and then an inner one with a red background. For some reason the outer (blue) layout is going maybe 70dp further than the bottom of the data, I'm guessing the pager is manipulating its height behind-the-scenes. The inner one (red) behaves correctly and wraps the data within it, so this is the one I'm trying to calculate the height of so I can update the viewpager's height, however its height is incorrectly being calculated as bigger than it is, so after I update the viewpager height there is empty space at the bottom. See the screenshot, as it probably makes more sense than what I just typed:</p>
  
  <p><img src="https://i.stack.imgur.com/KRSWf.png" alt="enter image description here"></p>
  
  <p>Another acceptable solution would be to calculate the longest page's height and set the viewpager to that once, and then don't update it as the page changes, but I haven't gone that route yet and would still run into the same issue calculating the correct height.</p>
  
  <p>I've spent a day on this already, any ideas / suggestions for how I can accurately update the viewpager height to fit its contents would be appreciated. I've tried a lot of other solutions on SO and haven't found one that actually worked. Here is where I've gotten so far with it:</p>
  
  <p>Pager view:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/property_details_pager"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Pager adapter: (SetPrimaryItem method is where I try to update the pager's height dynamically)</p>
  
  <pre><code>public class PropertyDetailsPagerAdapter : PagerAdapter {
          private ViewPager pager;
          private List&lt;List&lt;KeyValuePair&lt;string, string&gt;&gt;&gt; lists;
  
          public PropertyDetailsPagerAdapter (View view, List&lt;List&lt;KeyValuePair&lt;string, string&gt;&gt;&gt; lists) {
              this.pager = view.FindViewById&lt;ViewPager&gt; (Resource.Id.property_details_pager);
              this.pager.OffscreenPageLimit = lists.Count;
              this.lists = lists;
          }
  
          public void Update (List&lt;List&lt;KeyValuePair&lt;string, string&gt;&gt;&gt; lists) {
              this.pager.Adapter = null;
  
              this.lists = lists;
  
              this.pager.Adapter = this;
              this.pager.Adapter.NotifyDataSetChanged ();
          }
  
          #region Setup Views
  
          public override Java.Lang.Object InstantiateItem (View collection, int position) {
              var layoutInflater = (LayoutInflater)ApplicationContext.Activity.GetSystemService (Context.LayoutInflaterService);
              var view = layoutInflater.Inflate (Resource.Layout.PropertyDetailsPagerItem, null);
              var pagerList = view.FindViewById&lt;LinearLayout&gt; (Resource.Id.property_details_pager_list); 
  
              var list = this.lists [position];
              new PropertyDetailsPagerListAdapter (pagerList, Resource.Layout.PropertyDetailsPagerKeyValueRow, list);
  
              var listViewSwipe = view.FindViewById&lt;LinearLayout&gt; (Resource.Id.property_details_pager_swipe);
              listViewSwipe.Visibility = this.lists.Count &gt; 1 ? ViewStates.Visible : ViewStates.Gone;
  
              this.pager.AddView(view);
  
              return view;
          }
  
          public override void SetPrimaryItem (View container, int position, Java.Lang.Object @object) {
              base.SetPrimaryItem (container, position, @object);
  
              var view = this.pager.GetChildAt (position);
              var innerView = view.FindViewById&lt;RelativeLayout&gt; (Resource.Id.property_details_pager_item);
              innerView.Measure (0, 0);
  
              var parameters = this.pager.LayoutParameters as RelativeLayout.LayoutParams;
              parameters.Height = innerView.MeasuredHeight;// TODO: Why isn't this height accurate?!?!
              this.pager.LayoutParameters = parameters;
          }
  
          #endregion
  
          #region Infrastructure
  
          // Other irrelevant methods
  
          public override int Count {
              get {
                  return this.lists.Count;
              }
          }
  
          #endregion
  
          public List&lt;List&lt;KeyValuePair&lt;string, string&gt;&gt;&gt; Lists { get { return this.lists; } }
      }
  </code></pre>
  
  <p>View for each page within the pager:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/blue"&gt;
      &lt;RelativeLayout
          android:id="@+id/property_details_pager_item"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/red"&gt;
          &lt;LinearLayout
              android:id="@+id/property_details_pager_swipe"
              android:layout_below="@+id/dotted"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:visibility="gone"&gt;
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:visibility="invisible" /&gt;
              &lt;RelativeLayout
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:background="@color/light_gray"&gt;
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="Swipe for\nmore records"
                      android:textColor="@color/gray"
                      android:gravity="right"
                      android:padding="@dimen/padding" /&gt;
              &lt;/RelativeLayout&gt;
          &lt;/LinearLayout&gt;
          &lt;LinearLayout
              android:id="@+id/property_details_pager_list"
              android:layout_below="@+id/property_details_pager_swipe"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Adapter for each page within the pager:</p>
  
  <pre><code>public class PropertyDetailsPagerListAdapter
      {
          private int resourceId;
          private List&lt;KeyValuePair&lt;string, string&gt;&gt; keyValuePairs;
  
          public PropertyDetailsPagerListAdapter (LinearLayout pagerList, int resourceId, List&lt;KeyValuePair&lt;string, string&gt;&gt; keyValuePairs) {
              this.resourceId = resourceId;
              this.keyValuePairs = keyValuePairs;
  
              pagerList.RemoveAllViews ();
              foreach (var keyValuePair in this.keyValuePairs) {
                  pagerList.AddView (GetView (keyValuePair));
              }
          }
  
          public View GetView (KeyValuePair&lt;string, string&gt; keyValuePair) {
              var layoutInflater = (LayoutInflater)ApplicationContext.Activity.GetSystemService (Context.LayoutInflaterService);
  
              var view = layoutInflater.Inflate(resourceId, null);
  
              // Assumes Resource.Layout.PropertyDetailsPagerKeyValueRow
              view.FindViewById&lt;TextView&gt; (Android.Resource.Id.Text1).Text = keyValuePair.Key;
              view.FindViewById&lt;TextView&gt; (Android.Resource.Id.Text2).Text = keyValuePair.Value;
  
              return view;
          }
      }
  </code></pre>