title: ViewPager height-adjust with AnimateOnLayoutChanges glitches to different page
body: <p>I have a ViewPager that is tacked onto the bottom of a header bar. Upon reaching the final page, the header bar actually shrinks to Visibility.GONE and so the viewpager page occupies the whole page. This isn't the part I need help with, but instead having it AnimateLayoutChanges = true on the layout file, it glitches to another page instead of staying on the same page.</p>
  
  <p>Switcher code looks like this (called onClick of a tab, or on PageChange)</p>
  
  <pre><code>private void switchTab(Integer tabNumber){
      if (tabNumber != currentTab) {
          currentTab = tabNumber;
          for (int x=0; x&lt;tab_holder.getChildCount(); x++){
              final int pos = x;
              FacilityTab tab = tabs.get(pos);
              if (x != tabNumber){
                  tab.setBackgroundColor(getResources().getColor(R.color.text_msg_title));
              }else{
                  tab.setBackgroundColor(getResources().getColor(R.color.tab_bar_pressed));
              }
          }
          try {
              tab_controller.smoothScrollTo(tab_holder.getChildAt(tabNumber).getLeft() - 100, 0);
          }catch (Exception e){
              e.printStackTrace();
          }
      }
      if (tabNumber == (tab_holder.getChildCount() - 1)){
          tab_controller.setVisibility(View.GONE);
          headerBar.setVisibility(View.GONE);
          pager.setCurrentItem(tabNumber);
      }else{
          tab_controller.setVisibility(View.VISIBLE);
          headerBar.setVisibility(View.VISIBLE);
      }
  
  }
  </code></pre>
  
  <p>XML looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:animateLayoutChanges="true"
      android:background="@color/background_color"&gt;
  
      &lt;TableRow
          android:layout_width="match_parent"
          android:layout_height="52dp"
          android:background="@color/blue"
          android:elevation="2dp"
          android:id="@+id/header_bar"
          android:weightSum="1"
          android:visibility="visible"&gt;
  
          &lt;ImageView
              android:layout_width="50dp"
              android:layout_height="match_parent"
              android:src="@drawable/back_arrow"
              android:id="@+id/back_button"
              android:padding="18dp"
              android:tint="@color/white" /&gt;
  
          &lt;ImageView
              android:layout_width="100dp"
              android:layout_height="match_parent"
              android:id="@+id/imageView2"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:src="@drawable/chat_small"
              android:padding="8dp"
              android:layout_marginRight="50dp" /&gt;
  
      &lt;/TableRow&gt;
  
      &lt;HorizontalScrollView
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:layout_centerHorizontal="true"
          android:layout_below="@+id/header_bar"
          android:id="@+id/tab_controller"
          android:gravity="center"
          android:scrollbars="none"
          android:background="@color/text_msg_title"
          android:visibility="visible"&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/tab_holder"&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/HorizontalScrollView&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_below="@+id/tab_controller" /&gt;
  
      &lt;RelativeLayout
          android:layout_height="0dp"
          android:layout_width="match_parent"
          android:layout_alignParentBottom="true"
          android:layout_alignParentEnd="false"
          android:id="@+id/notification_view"
          android:layout_alignTop="@+id/varbutton"
          android:layout_marginTop="32dp"
          android:background="@color/notification_green"
          android:visibility="visible"&gt;
  
          &lt;ImageView
              android:layout_width="50dp"
              android:layout_height="match_parent"
              android:id="@+id/imageView"
              android:src="@drawable/notification_bell"
              android:padding="12dp" /&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="Username"
              android:id="@+id/notification_title"
              android:layout_alignParentTop="true"
              android:textColor="@color/white"
              android:gravity="center_vertical"
              android:layout_alignParentEnd="true"
              android:layout_toEndOf="@+id/imageView"
              android:textSize="24dp" /&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="User&amp;apos;s new message"
              android:id="@+id/notification_detail"
              android:layout_alignParentBottom="true"
              android:layout_alignParentEnd="true"
              android:layout_toEndOf="@+id/imageView"
              android:layout_marginBottom="2dp"
              android:textColor="@color/white"
              android:textSize="16sp"
              android:layout_below="@+id/notification_title"
              android:gravity="center_vertical" /&gt;
      &lt;/RelativeLayout&gt;
  
      &lt;ImageButton
          android:id="@+id/varbutton"
          android:layout_width="64dp"
          android:layout_height="64dp"
          android:layout_gravity="bottom|right"
          android:tint="@android:color/white"
          android:elevation="2dp"
          android:background="@drawable/floating_button"
          android:layout_marginBottom="24dp"
          android:layout_marginRight="24dp"
          android:src="@drawable/msgicon"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_marginTop="5dp"
          android:scaleType="fitCenter"
          android:padding="16dp"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>(Pictures edited for privacy)</p>
  
  <p>This is what a normal page looks like...
  <a href="https://i.stack.imgur.com/WgWnZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WgWnZ.png" alt="Here&#39;s a normal tab page"></a></p>
  
  <p>This is what happens when you TRY to swipe to the right, it swipes over then glitches back to this position with no header.
  <a href="https://i.stack.imgur.com/c6rmM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c6rmM.png" alt="Here&#39;s after swiping"></a></p>
  
  <p>But if you swipe right again from that glitched view, it'll do it properly. Why is it glitching and hanging up after successfully swiping all the way over?
  <a href="https://i.stack.imgur.com/HSs4Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HSs4Z.png" alt="Here&#39;s after swiping again, and the end goal."></a></p>