【问题标题】:Android: animateLayoutChanges not working properly with CoordinatorLayoutAndroid:animateLayoutChanges 无法与 CoordinatorLayout 一起正常工作
【发布时间】:2016-07-05 19:19:13
【问题描述】:

我一直在尝试在我的 Android 应用中使用协调器布局。我在协调器布局中有一个应用栏布局和一个嵌套滚动视图。在我的嵌套滚动视图中,我有一个线性布局,其中 animateLayoutChanges 为 true。

我的问题是,当线性布局高度的高度在使项目可见性为可见时增加时,线性布局会在 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- 这是问题所在。我点击了显示。现在,由于动画布局更改的动画,线性布局已移动到 App Bar 布局下。如您所见,显示按钮已移至应用栏下方。

图 3- 现在当我触摸屏幕或滚动时,滚动变得正常。

请帮忙。这几天我一直在尝试解决这个问题。谢谢。

【问题讨论】:

    标签: android android-coordinatorlayout android-nestedscrollview


    【解决方案1】:

    我遇到了同样的问题:包含 LinearLayout 且 animateLayoutChanges 设置为 true 的 NestedScrollView 导致内容出现滚动问题。就我而言,内容在 appBarLayout 下滑动。

    此错误在此处记录为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"
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2020-08-19
      • 2013-02-06
      • 2019-02-02
      • 2021-10-15
      • 2016-09-13
      相关资源
      最近更新 更多