Android: animateLayoutChanges not working properly with CoordinatorLayout

Viewed 3805

I have been trying to use a Coordinator Layout in my android app. I have an app bar layout and a nested scroll view in coordinator layout. In my nested scroll view I have a Linear layout with animateLayoutChanges as true.

My problem is that when ever the height of Linear layout height increases on making the items visibility as Visible, the Linear layout goes under Appbar Layout. Only after clicking the screen or scrolling, the proper scrolling effect takes places.

I have created a simple application to show the issue. Below is the layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context="testapp.test.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:animateLayoutChanges="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:animateLayoutChanges="true"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:animateLayoutChanges="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show"
            android:id="@+id/test_Button"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hide"
            android:id="@+id/test_Button2"/>
        <TextView
            android:id="@+id/test_tv"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:visibility="gone"
            android:background="@color/colorAccent"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

In this on clicking the show button I am making the Textview visible. Please see pictures to understand my problem.

Pic 1- Initial State.

Pic 2- Here is the problem. I have clicked Show. Now the Linear layout has moved under App Bar Layout due to animation by animate layout changes. As you can see, Show button has moved under App Bar.

Pic 3- Now when I touch screen or scroll, the scrolling becomes proper.

Please help. I have been trying to fix this for days. Thanks.

Initial State.

Here is the problem. I have clicked Show. Now the Linear layout has moved under App Bar Layout due to animation. Now when I touch screen or scroll, the scrolling becomes proper.

1 Answers
Related