Android:使用CoordinatorLayout时animateLayoutChanges无法正常工作

6
我一直在尝试在我的Android应用程序中使用Coordinator Layout。我有一个应用栏布局和一个嵌套滚动视图在协调布局中。在我的嵌套滚动视图中,我有一个线性布局,其中animateLayoutChanges设置为true。
我的问题是,在线性布局高度增加时,并将项目的可见性设置为Visible时,线性布局会进入Appbar布局下方。只有在点击屏幕或滚动后,才会出现正确的滚动效果。
我创建了一个简单的应用程序来显示这个问题。下面是布局。
<?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>

在点击显示按钮时,我将TextView设置为可见状态。请看图片以理解我的问题。
图片1- 初始状态。
图片2- 这就是问题所在。我已经点击了Show按钮。现在由于布局变化动画,线性布局移动到了AppBar Layout下面。如您所见,Show按钮已经移动到了App Bar下面。
图片3- 现在当我触摸屏幕或滚动时,滚动变得正常。
请帮忙。我一直在努力解决这个问题。谢谢。 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个回答

6
我遇到了相同的问题:一个包含animateLayoutChanges属性为true的LinearLayout的NestedScrollView会导致内容的滚动问题。在我的情况下,内容会滑动到appBarLayout下方。
这个bug在issue 180504中有记录。根据最新的支持库23.2.0,这个问题现在已经解决了,因此更新到这个版本应该就可以解决问题:
ext {
    supportLibraryVersion = "23.2.0"
}

dependencies {
    ...

    // this is the primary dependency for coordinator layout
    // but, of course, update all that depend on the support library
    // note: the design library depends on the Support v4 and AppCompat Support Libraries
    //       those will be included automatically when you add the Design library dependency
    compile "com.android.support:design:$supportLibraryVersion"

    ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接